I have a method which I use on occasion to convert strings (like enum
and property names) to PascalCase
for display.
It's fairly simple, so there's probably not much to comment on, but as usual I appreciate any advice on improving it.
/// <summary>
/// Converts a string of dash-separated, or underscore-separated words to a PascalCase string.
/// </summary>
/// <param name="s">The string to convert.</param>
/// <returns>The resulting PascalCase string.</returns>
public static string ToPascalCase(this string s)
{
string[] words = s.Split(new char[2] { '-', '_' }, StringSplitOptions.RemoveEmptyEntries);
StringBuilder sb = new StringBuilder(words.Sum(x => x.Length));
foreach (string word in words)
{
sb.Append(word[0].ToString().ToUpper() + word.Substring(1));
}
return sb.ToString();
}
Feel free to use it if you like, as per my usual regards.