Created
November 8, 2013 17:08
-
-
Save gabrieljoelc/7374242 to your computer and use it in GitHub Desktop.
Pascal case to string with spaces! From http://trycatchfail.com/blog/post/Building-Nice-Display-Names-From-Pascal-Case-View-Model-Names-in-ASPNET-MVC-3.aspx and http://popcyclical.com/2010/09/12/SplittingPascalCamelCaseWithRegExEnhancements.aspx
This file contains hidden or 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 StringExtensions | |
{ | |
public static string ToStringWithSpaces(this string input) | |
{ | |
return Regex.Replace( | |
input, | |
"(?<!^)" + // don't match on the first character - never want to place a space here | |
"(" + | |
" [A-Z][a-z] |" + // put a space before "Aaaa" | |
" (?<=[a-z])[A-Z] |" + // put a space into "aAAA" before the first capital | |
" (?<![A-Z])[A-Z]$" + // if the last letter is capital, prefix it with a space too | |
")", | |
" $1", | |
RegexOptions.IgnorePatternWhitespace); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment