是否有 String.Split() 考虑了 StringComparison?

Is there String.Split() which takes StringComparison into account?

Best Practices for Using Strings in the .NET Framework we are encouraged to supply proper StringComparison whenever we are comparing strings. I agree with the point but I see that unlike other methods, String.Split()中实际上没有带比较参数的重载。

是否有等同于 String.Split() 在框架中的某处进行字符串比较,或者我应该自己编写?

Is there equivalent of String.Split() taking string comparisons somewhere in the framework?

没有。那没有。坦率地说,我认为这没有多大意义。如果您在特殊字符上拆分字符串,通常是因为另一个系统向您提供了原始字符串,那么您为什么要在 Xx 上拆分?通常您不想这样做,并且 .NET 不提供帮助您处理它的方法。

Am I expected to write my own?

嗯,你需要一点帮助。这是一个不区分大小写的拆分器。它仍然需要一些工作,但您可以将其用作起点:

public static string[] Split(string s, params char[] delimeter)
{
    List<string> parts = new List<string>();

    int lastPartIndex = 0;
    for (int i = 0; i < s.Length; i++)
    {
        if (delimeter.Select(x => char.ToUpperInvariant(x)).Contains(char.ToUpperInvariant(s[i])))
        {
            parts.Add(s.Substring(lastPartIndex, i - lastPartIndex));

            lastPartIndex = i + 1;
        }
    }

    if (lastPartIndex < s.Length)
    {
        parts.Add(s.Substring(lastPartIndex, s.Length - lastPartIndex));
    }

    return parts.ToArray();
}

最接近的是Regex.Split。它可以忽略大小写和文化。 示例:Regex.Split("FirstStopSecondSTOPThird", "stop", RegexOptions.IgnoreCase)

将导致:

First
Second
Thrid