Last active
December 18, 2015 00:28
-
-
Save AmitKB/5696469 to your computer and use it in GitHub Desktop.
Make a string title or proper case
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 class StringHelper | |
{ | |
public static string ToTitleCase(this string s) | |
{ | |
// Return empty if input is null or empty | |
if (string.IsNullOrEmpty(s)) return string.Empty; | |
// This will hold our processing value | |
// and finally have string in title case. | |
var sb = new StringBuilder(); | |
// Do we need to make next character upper case | |
// First chracter should be upper case so | |
// default value is 'true' for this flag | |
bool nextUpper = true; | |
// Enumerate through each letter in the string/text | |
for (int idx = 0; idx < s.Length; idx++) | |
{ | |
// Get the current letter | |
string curLetter = s[idx].ToString(); | |
// If flag is true make upper otherwise lower | |
if (nextUpper) | |
sb.Append(curLetter.ToUpper()); | |
else | |
sb.Append(curLetter.ToLower()); | |
// If current letter was a blank space | |
// we need to make next letter upper case | |
nextUpper = (curLetter == " "); | |
} | |
// Finally return the new title case string/text | |
return sb.ToString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment