StringUtil

The StringUtil class has static methods for working with identifiers and words. It's part of the CodeSmith.Engine assembly. To use it, reference the assembly and import its namespace:

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

These are the methods you'll use most:

Method Description
IsPlural Returns true if a word is plural.
IsSingular Returns true if a word is singular.
ToPlural Converts a word to its plural form.
ToSingular Converts a word to its singular form.
ToCamelCase Converts a set of words to a single camel case identifier.
ToPascalCase Converts a set of words to a single Pascal case identifier.
ToSpacedWords Converts a camel case identifier to separate words.

For the complete list, see StringUtil Methods in the API documentation.

Note

CodeSmith.BaseTemplates also contains a StringUtility class with similar methods. It's obsolete; use StringUtil instead.

Plural overrides

Pluralization follows simple rules, so irregular words need help. IsPlural, IsSingular, ToPlural and ToSingular check a map of overrides first. By default they use the Plural-Overrides map that ships with CodeSmith Generator, which you can edit with the Map Editor. Each method also has overloads that take a different map, either as a map file name or as a MapCollection.

For example, this template declares two maps from a custom Plural.csmap file, which maps singular words to their plural forms. PluralOverrides reads it as is, and SingleOverrides reads it in reverse:

<%@ CodeTemplate Language="C#" TargetLanguage="Text" Description="Plural overrides." %>
<%@ Map Name="PluralOverrides" Src="Plural.csmap" Reverse="False" Description="Singular to plural overrides." %>
<%@ Map Name="SingleOverrides" Src="Plural.csmap" Reverse="True" Description="Plural to singular overrides." %>

With the built-in overrides:

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

With the maps declared by the Map directives:

Plural of Knife: <%= StringUtil.ToPlural("Knife", PluralOverrides) %>
Singular of Knives: <%= StringUtil.ToSingular("Knives", SingleOverrides) %>

With the map file name:

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