本文介绍了拆分PascalCase字符串转换成单独的词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找一种方式来分割PascalCase字符串,如: MyString的,成为单独的词 - 我的,字符串。 href="http://stackoverflow.com/questions/614930/how-can-i-cut1-camelcase-words">合影庆典问题的另一个用户中其接受的答案涵盖了你想要的,包括在一排数字和几个大写字母。虽然该样品具有的话开始大写,它这同样有效,当第一个字是小写。

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字符串转换成单独的词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 20:10