Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert PascalCase to kebab-case with C#?

Tags:

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"

like image 231
Mikhail Shilkov Avatar asked May 18 '16 13:05

Mikhail Shilkov


People also ask

What is Pascal case in JavaScript?

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.


1 Answers

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();     } } 
like image 126
Mikhail Shilkov Avatar answered Oct 21 '22 09:10

Mikhail Shilkov