代码之家  ›  专栏  ›  技术社区  ›  Kimtho6

拆分大写字符串[重复]

  •  10
  • Kimtho6  · 技术社区  · 15 年前

    可能的重复:
    Split a PascalCase string into separate words
    is there a elegant way to parse a word and add spaces before capital letters

    “左上”到“上”和“左”

    2 回复  |  直到 9 年前
        1
  •  13
  •   Brian Mains    9 年前

    如果你希望它是动态的,这意味着每次你发现一个大写字母将它分开,我不相信这是内置的,但可能是错误的;编写一个扩展方法已经足够容易了。

    string output = "";
    
    foreach (char letter in str)
    {
       if (Char.IsUpper(letter) && output.Length > 0)
         output += " " + letter;
       else
         output += letter;
    }
    
        2
  •  0
  •   Peter van Kekem    15 年前
            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;
            }
    
    推荐文章