在大写字母之间创建空格并跳过连续字母之间的空格

本文关键字:之间 空格 连续 大写字母 创建 | 更新日期: 2023-09-27 18:29:50

我找到了将空间"ThisCourse"创建为"This Course"的方法

按(EtienneT)LINQ语句在大写字母前添加空格

但我不能

在这个"ThisCourseID"answers"ThisCourse ID"之间创建空格,在"ID"之间没有空格

林克有办法做到这一点吗??

在大写字母之间创建空格并跳过连续字母之间的空格

好吧,如果它必须是单个linq语句。。。

var s = "ThisCourseIDMoreXYeahY";
s = string.Join(
        string.Empty, 
        s.Select((x,i) => (
             char.IsUpper(x) && i>0 && 
             ( char.IsLower(s[i-1]) || (i<s.Count()-1 && char.IsLower(s[i+1])) )
        ) ? " " + x : x.ToString()));
Console.WriteLine(s);

输出:"This Course ID More X Yeah Y"

var s = "ThisCourseID";
for (var i = 1; i < s.Length; i++)
{
    if (char.IsLower(s[i - 1]) && char.IsUpper(s[i]))
    {
        s = s.Insert(i, " ");
    }
}
Console.WriteLine(s); // "This Course ID"

如果你打算在很长的字符串上使用StringBuilder,你可以使用它来改进这一点,但就你的目的而言,正如你所展示的,它应该可以很好地工作。

修复:

var s = "ThisCourseIDSomething";
for (var i = 1; i < s.Length - 1; i++)
{
    if (char.IsLower(s[i - 1]) && char.IsUpper(s[i]) ||
        s[i - 1] != ' ' && char.IsUpper(s[i]) && char.IsLower(s[i + 1]))
    {
        s = s.Insert(i, " ");
    }
}
Console.WriteLine(s); // This Course ID Something

您不需要LINQ,但您可以"枚举"并使用lambda使其更通用
(尽管不确定这些是否有意义)

static IEnumerable<string> Split(this string text, Func<char?, char?, char, int?> shouldSplit)
{
    StringBuilder output = new StringBuilder();
    char? before = null;
    char? before2nd = null;
    foreach (var c in text)
    {
        var where = shouldSplit(before2nd, before, c);
        if (where != null)
        {
            var str = output.ToString();
            switch(where)
            {
                case -1:
                    output.Remove(0, str.Length -1);
                    yield return str.Substring(0, str.Length - 1);
                    break;
                case 0: default:
                    output.Clear();
                    yield return str;
                    break;
            }
        }
        output.Append(c);
        before2nd = before;
        before = c;
    }
    yield return output.ToString();
}

并这样称呼它,例如…

    static IEnumerable<string> SplitLines(this string text)
    {
        return text.Split((before2nd, before, now) =>
        {
            if ((before2nd ?? 'A') == ''r' && (before ?? 'A') == ''n') return 0; // split on 'now'
            return null; // don't split
        });
    }
    static IEnumerable<string> SplitOnCase(this string text)
    {
        return text.Split((before2nd, before, now) =>
        {
            if (char.IsLower(before ?? 'A') && char.IsUpper(now)) return 0; // split on 'now'
            if (char.IsUpper(before2nd ?? 'a') && char.IsUpper(before ?? 'a') && char.IsLower(now)) return -1; // split one char before
            return null; // don't split
        });
    }

在某个地方。。。

        var text = "ToSplitOrNotToSplitTHEQuestionIsNow";
        var words = text.SplitOnCase();
        foreach (var word in words)
            Console.WriteLine(word);
        text = "To'r'nSplit'r'nOr'r'nNot'r'nTo'r'nSplit'r'nTHE'r'nQuestion'r'nIs'r'nNow";
        words = text.SplitLines();
        foreach (var word in words)
            Console.WriteLine(word);

:)