拆分PascalCase字符串转换成单独的词 [英] Split a PascalCase string into separate words
问题描述
我要寻找一种方式来分割PascalCase字符串,如: MyString的,成为单独的词 - 我的,字符串。 href="http://stackoverflow.com/questions/614930/how-can-i-cut1-camelcase-words">合影庆典
问题的另一个用户,但我想知道如何与一般的普通恩pressions或至少在.NET中做到这一点。
I am looking for a way to split PascalCase strings, e.g. "MyString", into separate words - "My", "String". Another user posed the question for bash
, but I want to know how to do it with general regular expressions or at least in .NET.
奖金,如果你能找到一种方法,也分裂(和可选的利用)驼峰字符串:例如, myString中变成了我和字符串,另外还可以选择利用/小写一方或双方的字符串。
Bonus if you can find a way to also split (and optionally capitalize) camelCase strings: e.g. "myString" becomes "my" and "String", with the option to capitalize/lowercase either or both of the strings.
推荐答案
看到这一问题:<一href="http://stackoverflow.com/questions/3103730/is-there-a-elegant-way-to-parse-a-word-and-add-spaces-before-capital-letters">Is有一个优雅的方式来解析一个字之前大写字母加空格?中其接受的答案涵盖了你想要的,包括在一排数字和几个大写字母。虽然该样品具有的话开始大写,它这同样有效,当第一个字是小写。
See this question: Is there a elegant way to parse a word and add spaces before capital letters? Its accepted answer covers what you want, including numbers and several uppercase letters in a row. While this sample has words starting in uppercase, it it equally valid when the first word is in lowercase.
string[] tests = {
"AutomaticTrackingSystem",
"XMLEditor",
"AnXMLAndXSLT2.0Tool",
};
Regex r = new Regex(
@"(?<=[A-Z])(?=[A-Z][a-z])|(?<=[^A-Z])(?=[A-Z])|(?<=[A-Za-z])(?=[^A-Za-z])"
);
foreach (string s in tests)
r.Replace(s, " ");
以上将输出:
The above will output:
[Automatic][Tracking][System]
[XML][Editor]
[An][XML][And][XSLT][2.0][Tool]
这篇关于拆分PascalCase字符串转换成单独的词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!