通过拆分大写字母串 [英] Split a string by capital letters
问题描述
可能重复:结果
的正则表达式,拆分字符串用大写字母,但忽略TLA
块引用>
我有一个字符串,它是几个单词的组合,每个单词都大写结果
,例如:SeveralWordsString
使用C#,我该如何分割字符串成一个聪明的办法?
谢谢!
几个字字符串< DIV CLASS =h2_lin>解决方案
使用这个表达式(我忘了从StackOverflow的答案,我采购的话,会搜索现在):
公共静态字符串ToLowercaseNamingConvention(这个字符串s,布尔与toLowerCase)
{
如果(与toLowerCase)
{
变种R =新的正则表达式( @
(= [AZ] [AZ]?)|
(LT = [AZ]);(?= [AZ])(LT?= [^ AZ])|
(小于?= [A-ZA-Z]),RegexOptions.IgnorePatternWhitespace)(= [^ A-ZA-Z]);
返回r.Replace(S ,_)ToLower将();
}
,否则
返回小号;
}
我用它在这个项目: HTTP: //www.ienablemuch.com/2010/12/intelligent-brownfield-mapping-system.html
我发现它现在:的如何转换成驼峰人类可读的名字在Java中?
精美分裂TodayILiveInTheUSAWithSimon,没有空间对今天前:
使用系统;使用System.Text.RegularExpressions
:
命名空间TestSplit
{
类MainClass
{
公共静态无效的主要(字串[] args)
{
Console.WriteLine(你好世界!);
变种R =新的正则表达式(@
(小于??= [AZ])(= [AZ] [AZ])|
; |
(LT = [^ AZ])(= [AZ])(小于?= [A-ZA-Z])(?= [^ A-ZA-Z]) ,RegexOptions.IgnorePatternWhitespace);
字符串s =TodayILiveInTheUSAWithSimon;
Console.WriteLine(YYY {0} ZZZ,r.Replace(S,));
}
}
}
输出:
YYYToday我住在美国有了SimonZZZ
Possible Duplicate:
Regular expression, split string by capital letter but ignore TLA
I have a string which is a combination of several words, each word is capitalized.
For example: SeveralWordsString
Using C#, how do I split the string into "Several Words String" in a smart way?
Thanks!
Use this regex (I forgot from which stackoverflow answer I sourced it, will search it now):
public static string ToLowercaseNamingConvention(this string s, bool toLowercase)
{
if (toLowercase)
{
var r = new Regex(@"
(?<=[A-Z])(?=[A-Z][a-z]) |
(?<=[^A-Z])(?=[A-Z]) |
(?<=[A-Za-z])(?=[^A-Za-z])", RegexOptions.IgnorePatternWhitespace);
return r.Replace(s, "_").ToLower();
}
else
return s;
}
I use it in this project: http://www.ienablemuch.com/2010/12/intelligent-brownfield-mapping-system.html
[EDIT]
I found it now: How do I convert CamelCase into human-readable names in Java?
Nicely split "TodayILiveInTheUSAWithSimon", no space on front of " Today":
using System;
using System.Text.RegularExpressions;
namespace TestSplit
{
class MainClass
{
public static void Main (string[] args)
{
Console.WriteLine ("Hello World!");
var r = new Regex(@"
(?<=[A-Z])(?=[A-Z][a-z]) |
(?<=[^A-Z])(?=[A-Z]) |
(?<=[A-Za-z])(?=[^A-Za-z])", RegexOptions.IgnorePatternWhitespace);
string s = "TodayILiveInTheUSAWithSimon";
Console.WriteLine( "YYY{0}ZZZ", r.Replace(s, " "));
}
}
}
Output:
YYYToday I Live In The USA With SimonZZZ
这篇关于通过拆分大写字母串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!