Skip to content

Instantly share code, notes, and snippets.

@benjaminramey
Created February 1, 2012 15:49
Show Gist options
  • Select an option

  • Save benjaminramey/1717658 to your computer and use it in GitHub Desktop.

Select an option

Save benjaminramey/1717658 to your computer and use it in GitHub Desktop.
This little method (in C#) splits a pascal-cased string along the word boundaries to put spaces between the words. A pascal-cased string is a string where each word boundary begins with a capital letter.
public static string SpacePascalCase(string input)
{
string splitString = String.Empty;
for (int idx = 0; idx < input.Length; idx++)
{
char c = input[idx];
if (Char.IsUpper(c)
// keeps abbreviations together like "Number HEI"
// instead of making it "Number H E I"
&& ((idx < input.Length - 1
&& !Char.IsUpper(input[idx + 1]))
|| (idx != 0
&& !Char.IsUpper(input[idx - 1])))
&& splitString.Length > 0)
{
splitString += " ";
}
splitString += c;
}
return splitString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment