开发者

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

开发者 https://www.devze.com 2023-03-27 07:30 出处:网络
This question already has answers here:开发者_开发技巧 Closed 11 years ago. Possible Duplicate: An algorithm to "spacify" CamelCased strings
This question already has answers here: 开发者_开发技巧 Closed 11 years ago.

Possible Duplicate:

An algorithm to "spacify" CamelCased strings

I have a string like this: 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!


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();


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


LINQ based approach:

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


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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消