StringUtil

To use the functions in the StringUtil class, you should set a reference to the CodeSmith.Engine assembly and import its namespace:

<%@ Assembly Name="CodeSmith.Engine" %>
<%@ Import Namespace="CodeSmith.Engine" %>

The StringUtil class includes these static methods:

  • IsPlural - returns True if a string is plural.
  • IsSingular - returns True if a string is singular
  • ToCamelCase - converts a set of words to a single camel case identifier
  • ToPlural - converts a word to its plural form
  • ToSingular - converts a word to its singular form
  • ToSpacedWords - converts a camel case identifier to separate words
For a complete listing of all StringUtil methods please refer to the API documentation.

Example

The following example will show how you can mapping overrides and StringUtil. StringUtil ToPlural and ToSinglular supports overriding the converted word with a special csmap file.  The default override file is called Plural-Overrides.csmap.  It will be used by default. However, you can use a different Map file if needed. 

<%@ CodeTemplate Language="C#" TargetLanguage="Text"
          Debug="False" Description="Plural Overrides." %>

<%@ Map Name="PluralOverrides"
          Src="Plural.csmap" Reverse="False"
          Description="Convert system data types to c# alias" %>
<%@ Map Name="SingleOverrides"
          Src="Plural.csmap" Reverse="True"
          Description="Convert system data types to c# alias" %>

Using the built in csmap

Child: <%= StringUtil.ToPlural("Child") %>
Children: <%= StringUtil.ToSingular("Children") %>

Using the MapCollection 

Knife: <%= StringUtil.ToPlural("nife", PluralOverrides) %>
Knives: <%= StringUtil.ToSingular("nives", SingleOverrides) %>

Using the csmap name

Knife: <%= StringUtil.ToPlural("nife", "Plural.csmap") %>
Knives: <%= StringUtil.ToSingular("nives", "Plural.csmap") %>