用大写字母拆分字符串

时间:2010-12-20 10:58:39

标签: c# string

  

可能重复:
  Regular expression, split string by capital letter but ignore TLA

我有一个字符串,它是几个单词的组合,每个单词都是大写的 例如:SomeWordsString

使用C#,如何以智能方式将字符串拆分为“几个字符串”?

谢谢!

6 个答案:

答案 0 :(得分:76)

使用这个正则表达式(我忘记了我从哪个stackoverflow回答来源,现在会搜索它):

 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;
        }

我在这个项目中使用它:http://www.ienablemuch.com/2010/12/intelligent-brownfield-mapping-system.html

<强> [编辑]

我现在找到了它:How do I convert CamelCase into human-readable names in Java?

很好地拆分“TodayILiveInTheUSAWithSimon”,“今天”前面没有空格:

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, " "));
        }
    }
}

输出:

 YYYToday I Live In The USA With SimonZZZ

答案 1 :(得分:56)

string[] SplitCamelCase(string source) {
    return Regex.Split(source, @"(?<!^)(?=[A-Z])");
}

样品:

https://dotnetfiddle.net/0DEt5m

答案 2 :(得分:22)

您可以循环浏览字符,并在需要的地方添加空格:

string theString = "SeveralWordsString";

StringBuilder builder = new StringBuilder();
foreach (char c in theString) {
  if (Char.IsUpper(c) && builder.Length > 0) builder.Append(' ');
  builder.Append(c);
}
theString = builder.ToString();

答案 3 :(得分:5)

    public static IEnumerable<string> SplitOnCapitals(string text)
    {
        Regex regex = new Regex(@"\p{Lu}\p{Ll}*");
        foreach (Match match in regex.Matches(text))
        {
            yield return match.Value;    
        }
    }

这将正确处理Unicode。

答案 4 :(得分:2)

            string str1 = "SeveralWordsString";
            string newstring = "";
            for (int i = 0; i < str1.Length; i++)
            {
                if (char.IsUpper(str1[i]))
                    newstring += " ";                    
                newstring += str1[i].ToString();
            }

答案 5 :(得分:1)