diff --git a/Apewer/TextUtility.cs b/Apewer/TextUtility.cs index d5928b0..4c7b208 100644 --- a/Apewer/TextUtility.cs +++ b/Apewer/TextUtility.cs @@ -2,6 +2,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Globalization; using System.Text; using System.Text.RegularExpressions; @@ -982,8 +983,9 @@ namespace Apewer return Regex.IsMatch(trim, "(^[0-9a-zA-Z_ -;:,~!@#%&=<>~\\(\\)\\.\\$\\^\\`\\'\\\"\\&\\{\\}\\[\\]\\|\\*\\+\\?]{0,80}$)"); } - /// 转换文本为驼峰形式。 - public static string Camel(string text) + /// 转换文本为驼峰形式,首字小写,其它词首字母大写。 + /// 示例:toCamelCase + public static string Camel(this string text) { if (string.IsNullOrEmpty(text) || !char.IsUpper(text[0])) return text; @@ -1008,6 +1010,99 @@ namespace Apewer return new string(chars); } + /// 转换文本为帕斯卡形式,所有词首字母大写。 + /// 示例:ToPascalCase + public static string Pascal(this string text) + { + if (string.IsNullOrEmpty(text)) return text; + + var sb = new StringBuilder(); + var snake = Snake(text); + var split = snake.Split('_'); + foreach (var word in split) + { + if (word.Length < 1) continue; + var chars = word.ToCharArray(); + chars[0] = char.ToUpper(chars[0]); + sb.Append(new string(chars)); + } + return sb.ToString(); + } + + /// 转换文本为串式形式,使用横线连接每个词。 + /// 示例:to-kebab-case + public static string Kebab(this string text) => WordCase(text, '-'); + + /// 转换文本为蛇形形式,使用下划线连接每个词。 + /// 示例:to_sname_case + public static string Snake(this string text) => WordCase(text, '_'); + + /// 转换单词风格。 + static string WordCase(string text, char separator) + { + if (string.IsNullOrEmpty(text)) return text; + + const int StateStart = 1; + const int StateLower = 2; + const int StateUpper = 3; + const int StateNewWord = 4; + + var sb = new StringBuilder(); + var state = StateStart; + + for (int i = 0; i < text.Length; i++) + { + if (text[i] == ' ') + { + if (state != StateStart) + { + state = StateNewWord; + } + } + else if (char.IsUpper(text[i])) + { + switch (state) + { + case StateUpper: + bool hasNext = (i + 1 < text.Length); + if (i > 0 && hasNext) + { + char nextChar = text[i + 1]; + if (!char.IsUpper(nextChar) && nextChar != separator) + { + sb.Append(separator); + } + } + break; + case StateLower: + case StateNewWord: + sb.Append(separator); + break; + } + + char c; + c = char.ToLower(text[i], CultureInfo.InvariantCulture); + sb.Append(c); + + state = StateUpper; + } + else if (text[i] == separator) + { + sb.Append(separator); + state = StateStart; + } + else + { + if (state == StateNewWord) sb.Append(separator); + + sb.Append(text[i]); + state = StateLower; + } + } + + return sb.ToString(); + } + /// 移除参数 chars 中的每一个字符。 public static string RemoveChars(string text, string chars) { diff --git a/Apewer/_Extensions.cs b/Apewer/_Extensions.cs index 27d48a2..7287871 100644 --- a/Apewer/_Extensions.cs +++ b/Apewer/_Extensions.cs @@ -117,9 +117,6 @@ public static class Extensions #region String、StringBuilder - /// 转换文本为驼峰形式。 - public static string Camel(this string @this) => TextUtility.Camel(@this); - /// 转换为 Boolean 值。 public static bool Boolean(this string @this) => NumberUtility.Boolean(@this);