I needed to convert some Pascal case strings to human readable strings. Basically I have an enum containing errors, but wanted a nice way to convert them straight to messages for the exceptions that would be thrown.
Here’s how I did it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| public static string ToHumanFromPascal(this string s)
{
var sb = new StringBuilder();
var ca = s.ToCharArray();
sb.Append(ca[0]);
for (int i = 1; i < ca.Length - 1; i++)
{
char c = ca[i];
if (char.IsUpper(c) && (char.IsLower(ca[i + 1]) || char.IsLower(ca[i - 1])))
{
sb.Append(' ');
}
sb.Append(c);
}
sb.Append(ca[ca.Length - 1]);
return sb.ToString();
} |
Posted by darkhelmet under Computers & Programming | No Comments »