How do I convert PascalCase to kebab-case with C#?
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();
}
}
Here's my solution using Microsoft's capitalization conventions. https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/capitalization-conventions
public static class StringExtensions
{
public static string PascalToKebabCase(this string source)
{
if (source is null) return null;
if (source.Length == 0) return string.Empty;
StringBuilder builder = new StringBuilder();
for (var i = 0; i < source.Length; i++)
{
if (char.IsLower(source[i])) // if current char is already lowercase
{
builder.Append(source[i]);
}
else if (i == 0) // if current char is the first char
{
builder.Append(char.ToLower(source[i]));
}
else if (char.IsDigit(source[i]) && !char.IsDigit(source[i - 1])) // if current char is a number and the previous is not
{
builder.Append('-');
builder.Append(source[i]);
}
else if (char.IsDigit(source[i])) // if current char is a number and previous is
{
builder.Append(source[i]);
}
else if (char.IsLower(source[i - 1])) // if current char is upper and previous char is lower
{
builder.Append('-');
builder.Append(char.ToLower(source[i]));
}
else if (i + 1 == source.Length || char.IsUpper(source[i + 1])) // if current char is upper and next char doesn't exist or is upper
{
builder.Append(char.ToLower(source[i]));
}
else // if current char is upper and next char is lower
{
builder.Append('-');
builder.Append(char.ToLower(source[i]));
}
}
return builder.ToString();
}
}
Test
string[] stringArray = new[]
{
null,
"",
"I",
"IO",
"FileIO",
"SignalR",
"IOStream",
"COMObject",
"WebAPI",
"Windows10",
"WindowsServer2019R2"
};
foreach (var str in stringArray)
{
Console.WriteLine($"{str} --> {str.PascalToKebabCase()}");
}
// Output:
// -->
// -->
// I --> i
// IO --> io
// FileIO --> file-io
// SignalR --> signal-r
// IOStream --> io-stream
// COMObject --> com-object
// WebAPI --> web-api
// Windows10 --> windows-10
// WindowsServer2016R2 --> windows-server-2016-r-2