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"
Pascal case -- or PascalCase -- is a programming naming convention where the first letter of each compound word in a variable is capitalized. The use of descriptive variable names is a software development best practice. However, modern programming languages do not allow variables names to include blank spaces.
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-Z])", "-$1", RegexOptions.Compiled) .Trim() .ToLower(); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With