Skip to content

Instantly share code, notes, and snippets.

@AmitKB
Last active December 18, 2015 00:28
Show Gist options
  • Save AmitKB/5696469 to your computer and use it in GitHub Desktop.
Save AmitKB/5696469 to your computer and use it in GitHub Desktop.
Make a string title or proper case
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