如何使用 C# 将 PascalCase 转换为 kebab-case?
How do I convert PascalCase to kebab-case with C#?
如何使用 C# 将 PascalCase(也称为 UpperCamelCase)中的字符串值转换为 kebab 大小写?
例如"VeryLongName"
至 "very-long-name"
以下是使用正则表达式的方法:
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])",
"-",
RegexOptions.Compiled)
.Trim()
.ToLower();
}
}
这是一种不使用正则表达式的方法:
public static string PascalToKebabCase(this string str)
{
if (string.IsNullOrEmpty(str))
return string.Empty;
var builder = new StringBuilder();
builder.Append(char.ToLower(str.First()));
foreach (var c in str.Skip(1))
{
if (char.IsUpper(c))
{
builder.Append('-');
builder.Append(char.ToLower(c));
}
else
{
builder.Append(c);
}
}
return builder.ToString();
}
您对使用大写字母的众所周知的缩写有疑问。例如:COMObject
。这个解决方案显然行不通。
这是我使用 Microsoft 的大写约定的解决方案。 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();
}
}
测试
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
这是 Regex.Replace
的较短解决方案:
public static class KebabConverter
{
public static string ToKebabCase(this string str)
{
// find and replace all parts that starts with one capital letter e.g. Net
var str1 = Regex.Replace(str, "[A-Z][a-z]+", m => $"-{m.ToString().ToLower()}");
// find and replace all parts that are all capital letter e.g. NET
var str2 = Regex.Replace(str1, "[A-Z]+", m => $"-{m.ToString().ToLower()}");
return str2.TrimStart('-');
}
}
这是我从其他人那里获取一些代码和 Regex 的结果:
var pascalCase = "MyReally-CoolMFAString";
var dashCase = Regex.Replace(pascalCase, @"(?<!^)(?<!-)((?<=\p{Ll})\p{Lu}|\p{Lu}(?=\p{Ll}))", "-").ToLower();
Console.WriteLine(dashCase);
输出为:
my-really-cool-mfa-string
从@InBetween 获得灵感:
public static string PascalToKebabCase(this string str)
{
IEnumerable<char> ConvertChar(char c, int index)
{
if (char.IsUpper(c))
{
if (index != 0) yield return '-';
yield return char.ToLower(c);
}
else yield return c;
}
return string.Concat(str.SelectMany(ConvertChar));
}
从@johnathan-barclay 获得灵感:
string.Concat(str.Select((c, i) => (char.IsUpper(c) && i > 0 ? "-" : "") + char.ToLower(c)));
string input = "VeryLongName";
char[] arr = input.ToCharArray();
string output = Convert.ToString( arr[0]);
for(int i=1; i<arr.Length; i++){
if(char.IsUpper(arr[i]))
output = output + "-" + Convert.ToString( arr[i]);
else
output = output + Convert.ToString( arr[i]);
}
Console.WriteLine(output.ToLower());
如何使用 C# 将 PascalCase(也称为 UpperCamelCase)中的字符串值转换为 kebab 大小写?
例如"VeryLongName"
至 "very-long-name"
以下是使用正则表达式的方法:
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])",
"-",
RegexOptions.Compiled)
.Trim()
.ToLower();
}
}
这是一种不使用正则表达式的方法:
public static string PascalToKebabCase(this string str)
{
if (string.IsNullOrEmpty(str))
return string.Empty;
var builder = new StringBuilder();
builder.Append(char.ToLower(str.First()));
foreach (var c in str.Skip(1))
{
if (char.IsUpper(c))
{
builder.Append('-');
builder.Append(char.ToLower(c));
}
else
{
builder.Append(c);
}
}
return builder.ToString();
}
您对使用大写字母的众所周知的缩写有疑问。例如:COMObject
。这个解决方案显然行不通。
这是我使用 Microsoft 的大写约定的解决方案。 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();
}
}
测试
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
这是 Regex.Replace
的较短解决方案:
public static class KebabConverter
{
public static string ToKebabCase(this string str)
{
// find and replace all parts that starts with one capital letter e.g. Net
var str1 = Regex.Replace(str, "[A-Z][a-z]+", m => $"-{m.ToString().ToLower()}");
// find and replace all parts that are all capital letter e.g. NET
var str2 = Regex.Replace(str1, "[A-Z]+", m => $"-{m.ToString().ToLower()}");
return str2.TrimStart('-');
}
}
这是我从其他人那里获取一些代码和 Regex 的结果:
var pascalCase = "MyReally-CoolMFAString";
var dashCase = Regex.Replace(pascalCase, @"(?<!^)(?<!-)((?<=\p{Ll})\p{Lu}|\p{Lu}(?=\p{Ll}))", "-").ToLower();
Console.WriteLine(dashCase);
输出为:
my-really-cool-mfa-string
从@InBetween 获得灵感:
public static string PascalToKebabCase(this string str)
{
IEnumerable<char> ConvertChar(char c, int index)
{
if (char.IsUpper(c))
{
if (index != 0) yield return '-';
yield return char.ToLower(c);
}
else yield return c;
}
return string.Concat(str.SelectMany(ConvertChar));
}
从@johnathan-barclay 获得灵感:
string.Concat(str.Select((c, i) => (char.IsUpper(c) && i > 0 ? "-" : "") + char.ToLower(c)));
string input = "VeryLongName";
char[] arr = input.ToCharArray();
string output = Convert.ToString( arr[0]);
for(int i=1; i<arr.Length; i++){
if(char.IsUpper(arr[i]))
output = output + "-" + Convert.ToString( arr[i]);
else
output = output + Convert.ToString( arr[i]);
}
Console.WriteLine(output.ToLower());