Skip to content

Instantly share code, notes, and snippets.

@timiles
Created July 7, 2012 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timiles/3065794 to your computer and use it in GitHub Desktop.
Save timiles/3065794 to your computer and use it in GitHub Desktop.
ToUnderscoreCase / ToHyphenCase
/// <summary>
/// Converts PascalCase and camelCase to underscore_case. Leaves underscore_case unaffected.
/// </summary>
/// <param name="s">the string to convert</param>
/// <returns>the underscore_cased string</returns>
internal static string ToUnderscoreCase(this string s)
{
return Regex.Replace(s, @"(\p{Ll})(\p{Lu})", "$1_$2").ToLower();
}
/// <summary>
/// Converts PascalCase and camelCase to hyphen-case. Leaves hyphen-case unaffected.
/// </summary>
/// <param name="s">the string to convert</param>
/// <returns>the hyphen-cased string</returns>
internal static string ToHyphenCase(this string s)
{
return Regex.Replace(s, @"(\p{Ll})(\p{Lu})", "$1-$2").ToLower();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment