Skip to content

Instantly share code, notes, and snippets.

@RChehowski
Created April 29, 2017 10:03
Show Gist options
  • Save RChehowski/3544e31f1cebd73e0b91caee5ca608ae to your computer and use it in GitHub Desktop.
Save RChehowski/3544e31f1cebd73e0b91caee5ca608ae to your computer and use it in GitHub Desktop.
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