Skip to content

Instantly share code, notes, and snippets.

@LazZiya
Last active October 20, 2020 11:25
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 LazZiya/72b3f143fee0bcfa791652420b692ad7 to your computer and use it in GitHub Desktop.
Save LazZiya/72b3f143fee0bcfa791652420b692ad7 to your computer and use it in GitHub Desktop.
Convert strings to kebab case https://dotnetfiddle.net/WSE6sy
public static class KebabConverter
{
public static string ToKebabCase(this string str)
{
// find and replace all parts that starts with one capital letter
var str1 = Regex.Replace(str, "[A-Z][a-z]+", m => $"-{m.ToString().ToLower()}");
// find and replace all parts that are all capital letters
var str2 = Regex.Replace(str1, "[A-Z]+", m => $"-{m.ToString().ToLower()}");
return str2.TrimStart('-');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment