开发者

Splitting a string with uppercase [duplicate]

开发者 https://www.devze.com 2023-01-23 07:58 出处:网络
This question already has answers here: 开发者_StackOverflow社区 Closed 12 years ago. Possible Duplicates:
This question already has answers here: 开发者_StackOverflow社区 Closed 12 years ago.

Possible Duplicates:

Split a PascalCase string into separate words

is there a elegant way to parse a word and add spaces before capital letters

Is there a simple way to split this string "TopLeft" to "Top" and "Left"


If you want it dynamic, meaning every time you find an upper case letter break it apart, I don't believe this is built in, but could be wrong; it's easy enough to write an extension method.

string output = "";

foreach (char letter in str)
{
   if (Char.IsUpper(letter) && output.Length > 0)
     output += " " + letter;
   else
     output += letter;
}


        string s = "TopLeft";
        List<int> splits = new List<int>();
        for(int i=0; i<s.Length;i++)
            if(char.IsUpper(s[i]))
                splits.Add(i);

        int splitstart = 0;
        foreach (int split in splits)
        {
            s.Substring(splitstart, split);
            splitstart = split;
        }
0

精彩评论

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