|
|
|
@ -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}$)"); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>转换文本为驼峰形式。</summary>
|
|
|
|
public static string Camel(string text) |
|
|
|
/// <summary>转换文本为驼峰形式,首字小写,其它词首字母大写。</summary>
|
|
|
|
/// <remarks>示例:toCamelCase</remarks>
|
|
|
|
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); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>转换文本为帕斯卡形式,所有词首字母大写。</summary>
|
|
|
|
/// <remarks>示例:ToPascalCase</remarks>
|
|
|
|
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(); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>转换文本为串式形式,使用横线连接每个词。</summary>
|
|
|
|
/// <remarks>示例:to-kebab-case</remarks>
|
|
|
|
public static string Kebab(this string text) => WordCase(text, '-'); |
|
|
|
|
|
|
|
/// <summary>转换文本为蛇形形式,使用下划线连接每个词。</summary>
|
|
|
|
/// <remarks>示例:to_sname_case</remarks>
|
|
|
|
public static string Snake(this string text) => WordCase(text, '_'); |
|
|
|
|
|
|
|
/// <summary>转换单词风格。</summary>
|
|
|
|
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(); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>移除参数 chars 中的每一个字符。</summary>
|
|
|
|
public static string RemoveChars(string text, string chars) |
|
|
|
{ |
|
|
|
|