Created
November 18, 2014 17:10
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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