仅当下一个字符为小写时才将字符串拆分为大写

时间:2014-03-18 17:25:08

标签: c# string split

我找到了很多关于如何在大写字母上拆分字符串的例子,例如:

"MyNameIsRob"返回"My Name Is Rob"

我的情景有点不同......

我想完成以下任务:

"MyFavouriteChocIsDARKChocalate"应该返回"My Favourite Choc Is DARK Chocalate"

我能想到这样做的唯一方法是,如果下一个字符是小写的话,只将字符串拆分为upperacase。

关于如何实现这一目标的任何想法?

4 个答案:

答案 0 :(得分:5)

您可以使用lookahead和lookbehind进行正则表达式替换,以便在它之前或之后用小写字母查找大写...

var input = "MyFavouriteChocIsDARKChocalate";
var output = Regex.Replace(input, "(((?<!^)[A-Z](?=[a-z]))|((?<=[a-z])[A-Z]))", " $1");
Console.WriteLine(output);

http://dotnetfiddle.net/cIM6QG

答案 1 :(得分:2)

没有正则表达式,你可以使用它:

public static string SplitOnCaps(string s)

http://dotnetfiddle.net/qQIIgX

using System;
using System.Collections.Generic;
using System.Text;

public class Program
{
    public static void Main()
    {
        /*"MyFavouriteChocIsDARKChocalate" should return "My Favourite Choc Is DARK Chocalate"*/
        var input = "MyFavouriteChocIsDARKChocalate";
        var split = SplitOnCaps(input);
        Console.WriteLine(input + " --> " + split);
        var match = "My Favourite Choc Is DARK Chocalate";

        Console.WriteLine(split == match ? "Match" : "No Match");       
    }

    public static string SplitOnCaps(string s)
    {
        var splits = new List<int>();

        var chars = s.ToCharArray();

        for(var i=1; i<chars.Length-1; i++)
        {
            if (IsCapital(chars[i]) && !IsCapital(chars[i+1]) ||
               IsCapital(chars[i]) && !IsCapital(chars[i-1]))
            {
                splits.Add(i);
            }
        }

        var sb = new StringBuilder();

        var lastSplit = 0;
        foreach(var split in splits)
        {
            sb.Append(s.Substring(lastSplit, split - lastSplit) + " ");
            lastSplit = split;
        }
        sb.Append(s.Substring(lastSplit));

        return sb.ToString();
    }
    public static bool IsCapital(char c)
    {
        var i = (int)c;
        return i>=65 && i<=90;
    }
}
  

注意:我不熟练使用Regex

答案 2 :(得分:1)

也许经典方法符合您的需求:

private static string[] SplitSpecial(string s)
{
    List<string> tmp = new List<string>();
    int lastindex = 0;
    for (int i = 1; i < s.Length; i++)
        if (Char.IsUpper(s[i]) && (Char.IsLower(s[i - 1]) || (i < s.Length - 1 && Char.IsLower(s[i + 1]))))
        {
            if (i > lastindex)
                tmp.Add(s.Substring(lastindex, i - lastindex));
            lastindex = i;
        }
    tmp.Add(s.Substring(lastindex, s.Length - lastindex));
    return tmp.ToArray();
}

要制作单个字符串,您可以String.Join生成的数组:

string singleStr = String.Join(" ", SplitSpecial("MyFavouriteChocIsDARKChocalate"));

答案 3 :(得分:1)

经典方法+使用stringBuilder(如果使用长字符串,效率更高):

    string str = "MyFavouriteChocIsDARKChocalate";
    System.Text.StringBuilder output = new System.Text.StringBuilder(str.Substring(0,1));

    for (int i = 1; i < str.Length; i++)
    {
        if (Char.IsUpper(str[i]) && (!char.IsUpper(str[i-1]) || (i+1 < str.Length && char.IsLower(str[i+1]))))
        {
            output.Append(" " + str[i]);
        }
        else
        {
            output.Append(str[i]);
        }
    }
    string result = output.ToString();