Skip to content

Instantly share code, notes, and snippets.

@morbidcamel101
Created November 18, 2014 17:10
Show Gist options
  • Save morbidcamel101/13e57f1b5c8bb88d4801 to your computer and use it in GitHub Desktop.
Save morbidcamel101/13e57f1b5c8bb88d4801 to your computer and use it in GitHub Desktop.
Get a plural for the specified text. Works on 95% of english words.
public static string GetPlural(this string name)
{
if (string.IsNullOrEmpty(name))
{
return string.Empty;
}
name = name.Trim();
char lastChar = name[name.Length - 1];
switch (char.ToLower(lastChar))
{
case 'y':
if (name.Length > 1)
{
bool abort = false;
switch (char.ToLower(name[name.Length - 2]))
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'y':
abort = true;
break;
}
if (abort)
{
name += "s";
break;
}
}
name = name.Remove(name.Length - 1, 1);
name += "ies";
break;
case 's':
if (name.Length > 1)
{
switch (char.ToLower(name[name.Length - 2]))
{
case 's':
case 'u':
name += "es";
break;
}
}
break;
case 'h':
name += "es";
break;
default:
name += "s";
break;
}
return name;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment