【发布时间】:2011-04-26 20:59:57
【问题描述】:
是否有一个很好的功能来转动类似的东西
名字
到这里:
名字?
【问题讨论】:
-
这对我有用:
Regex.Replace(s, "([A-Z0-9]+)", " $1").Trim()。如果您想拆分每个大写字母,只需删除加号即可。
标签: c#
是否有一个很好的功能来转动类似的东西
名字
到这里:
名字?
【问题讨论】:
Regex.Replace(s, "([A-Z0-9]+)", " $1").Trim()。如果您想拆分每个大写字母,只需删除加号即可。
标签: c#
见:.NET - How can you split a "caps" delimited string into an array?
特别是:
Regex.Replace("ThisIsMyCapsDelimitedString", "(\\B[A-Z])", " $1")
【讨论】:
@"(\B[A-Z]+?(?=[A-Z][^A-Z])|\B[A-Z]+?(?=[^A-Z]))"
"HTMLGuide" → "H TML Guide"
Regex.Replace("ThisIsMy1stCapsDelimitedString", "(\\B[A-Z0-9])", " $1") 也可以拆分数字。
这是我广泛用于此类事情的扩展方法
public static string SplitCamelCase( this string str )
{
return Regex.Replace(
Regex.Replace(
str,
@"(\P{Ll})(\P{Ll}\p{Ll})",
"$1 $2"
),
@"(\p{Ll})(\P{Ll})",
"$1 $2"
);
}
它还处理像IBMMakeStuffAndSellIt 这样的字符串,将其转换为IBM Make Stuff And Sell It (IIRC)。
语法解释(credit):
{Ll} 是 Unicode 字符类别“字母小写”(与 {Lu}“字母大写”相对)。 P 是负匹配,而p 是正匹配,所以\P{Ll} 字面意思是“非小写”,p{Ll} 是“小写”。
所以这个正则表达式分成两种模式。 1:“大写、大写、小写”(将匹配 IBMMake 中的 MMa 并导致 IBM Make),以及 2.“小写,大写”(将匹配 MakeStuff 中的 eS )。这涵盖了所有驼峰式断点。
提示:用连字符替换空格并调用 ToLower 以生成 HTML5 数据属性名称。
【讨论】:
{Ll} 是 Unicode 字符类别“字母小写”(相对于 {Lu}“字母大写”)。 'P' 是否定匹配,而 'p' 是肯定匹配,所以\P{Ll} 字面意思是“非小写”,p{Ll} 是“小写”。所以这个正则表达式分成两种模式。 1:“大写、大写、小写”(将匹配“IBMMake”中的“MMa”并生成“IBM Make”),以及 2.“小写、大写”(匹配“MakeStuff”中的“eS” ')。这涵盖了所有骆驼案例断点。提示:用连字符替换空格并调用ToLower 以生成 html5 数据属性名称。
\p{Lu}代替\P{Ll}有什么问题吗?
最简单的方法:
var res = Regex.Replace("FirstName", "([A-Z])", " $1").Trim();
【讨论】:
你可以使用正则表达式:
Match ([^^])([A-Z])
Replace $1 $2
在代码中:
String output = System.Text.RegularExpressions.Regex.Replace(
input,
"([^^])([A-Z])",
"$1 $2"
);
【讨论】:
/// <summary>
/// Parse the input string by placing a space between character case changes in the string
/// </summary>
/// <param name="strInput">The string to parse</param>
/// <returns>The altered string</returns>
public static string ParseByCase(string strInput)
{
// The altered string (with spaces between the case changes)
string strOutput = "";
// The index of the current character in the input string
int intCurrentCharPos = 0;
// The index of the last character in the input string
int intLastCharPos = strInput.Length - 1;
// for every character in the input string
for (intCurrentCharPos = 0; intCurrentCharPos <= intLastCharPos; intCurrentCharPos++)
{
// Get the current character from the input string
char chrCurrentInputChar = strInput[intCurrentCharPos];
// At first, set previous character to the current character in the input string
char chrPreviousInputChar = chrCurrentInputChar;
// If this is not the first character in the input string
if (intCurrentCharPos > 0)
{
// Get the previous character from the input string
chrPreviousInputChar = strInput[intCurrentCharPos - 1];
} // end if
// Put a space before each upper case character if the previous character is lower case
if (char.IsUpper(chrCurrentInputChar) == true && char.IsLower(chrPreviousInputChar) == true)
{
// Add a space to the output string
strOutput += " ";
} // end if
// Add the character from the input string to the output string
strOutput += chrCurrentInputChar;
} // next
// Return the altered string
return strOutput;
} // end method
【讨论】:
正则表达式:
http://weblogs.asp.net/jgalloway/archive/2005/09/27/426087.aspx http://stackoverflow.com/questions/773303/splitting-camelcase
(可能是最好的 - 见第二个答案) http://bytes.com/topic/c-sharp/answers/277768-regex-convert-camelcase-into-title-case
从 UpperCamelCase 转换为 标题案例,使用这一行: Regex.Replace("UpperCamelCase",@"(\B[A-Z])",@" $1");
从 lowerCamelCase 转换 和 UpperCamelCase 到标题大小写,使用 MatchEvaluator:公共字符串 toTitleCase(匹配 m){ 字符 c=m.Captures[0].Value[0];返回 ((c>='a')&&(c
【讨论】: