Created
April 29, 2017 10:03
-
-
Save RChehowski/3544e31f1cebd73e0b91caee5ca608ae to your computer and use it in GitHub Desktop.
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
static string NameToDisplayString(string inDisplayName) | |
{ | |
// (?= # look-ahead: a position followed by... | |
// \p{Lu}\p{Ll} # an uppercase and a lowercase | |
// ) # | |
// | # or | |
// (?<= # look-behind: a position after... | |
// \p{Ll} # an uppercase | |
// ) # | |
// (?= # look-ahead: a position followed by... | |
// \p{Lu} # a lowercase | |
// ) # | |
string[] strings = Regex.Split(inDisplayName, @"(?=\p{Lu}\p{Ll})|(?<=\p{Ll})(?=\p{Lu})"); | |
StringBuilder sb = new StringBuilder(inDisplayName.Length * 2); | |
string[] articles = {"In", "As", "To", "Or", "At", "On", "If", "Be", "By", "The", "For", "And", "With", "When", "From"}; | |
strings.ToList().ForEach(s => | |
{ | |
if (!String.IsNullOrEmpty(s)) | |
{ | |
sb.Append(articles.Contains(s) ? s.ToLower() : s); | |
if (strings.Last() != s) | |
sb.Append(' '); | |
} | |
}); | |
return sb.ToString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment