从字符串[重复]获取大写字母的索引

[英]Get the Index of Upper Case letter from a String [duplicate]


Possible Duplicate:
An algorithm to "spacify" CamelCased strings

可能重复:一种“松散”CamelCased字符串的算法

I have a string like this: MyUnsolvedProblem

我有一个这样的字符串:MyUnsolvedProblem

I want to modify the string like this: My Unsolved Problem

我想修改这样的字符串:我未解决的问题

How can I do that? I have tried using Regex with no luck!

我怎样才能做到这一点?我尝试过使用正则表达式没有运气!

4 个解决方案

#1


11  

var result = Regex.Replace("MyUnsolvedProblem", @"(\p{Lu})", " $1").TrimStart();

Without regex:

没有正则表达式:

var s = "MyUnsolvedProblem";
var result = string.Concat(s.Select(c => char.IsUpper(c) ? " " + c.ToString() : c.ToString()))
    .TrimStart();

#2


5  

resultString = Regex.Replace("MyUnsolvedProblem", "([a-z])([A-Z])", "$1 $2");

#3


1  

I can offer a suggestion of how to do it in C# if that helps:

如果有帮助,我可以提供一个如何在C#中执行此操作的建议:

String PreString = "getAllItemsByID";

System.Text.StringBuilder SB = new System.Text.StringBuilder();

foreach (Char C in PreString)
{
    if (Char.IsUpper(C))
        SB.Append(' ');
    SB.Append(C);
}

Response.Write(SB.ToString());

I'm sure that there is a way to do it with regular expressions too, but this is one option.

我确信有一种方法可以使用正则表达式,但这是一个选项。

#4


1  

LINQ based approach:

基于LINQ的方法:

string data = "TestStringData";
var converted = data.Select(x => Char.IsUpper(x) ? String.Concat(" ", x) : x.ToString());
var singleString = converted.Aggregate((a, b) => a + b);

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2011/08/09/e2d004dd6f657ac321a2e39a81512c0b.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号