How do I convert a string value in PascalCase (other name is UpperCamelCase) to kebab-case with C#?
e.g. "VeryLongName"
to "very-long-name"
Here is how to do that with a regular expression:
public static class StringExtensions
{
public static string PascalToKebabCase(this string value)
{
if (string.IsNullOrEmpty(value))
return value;
return Regex.Replace(
value,
"(?<!^)([A-Z][a-z]|(?<=[a-z])[A-Z0-9])",
"-$1",
RegexOptions.Compiled)
.Trim()
.ToLower();
}
}
Note: the @RossK92's recommendation about handling numbers has been applied