It's a piece of code I wish I didn't use nearly as often I did, but because people like to do data entry with CAPSLOCK on, I feel the need to convert it to Proper Case.
Code Snippet
- public static string ProperCase(string stringInput)
- {
- System.Text.StringBuilder sb = new System.Text.StringBuilder();
- bool fEmptyBefore = true;
- foreach (char ch in stringInput)
- {
- char chThis = ch;
- if (Char.IsWhiteSpace(chThis))
- fEmptyBefore = true;
- else
- {
- if (Char.IsLetter(chThis) && fEmptyBefore)
- chThis = Char.ToUpper(chThis);
- else
- chThis = Char.ToLower(chThis);
- fEmptyBefore = false;
- }
- sb.Append(chThis);
- }
- return sb.ToString();
- }
It's not perfect - for example, It won't properly display my name, McGuire, if it's converting it from caps. But how would it know to capitalize the 'G'?