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

In the "Recommendations on the use of strings in the .NET Framework," we recommend delivering " when we compare strings. I agree with this, but I see that, unlike other methods, it actually does not have overloads with the comparison parameter. StringComparison String.Split()

Is there an equivalent String.Split()for comparing strings somewhere in a structure or should I write my own?

+4
source share
2 answers

The closest thing is Regex.Split. He can ignore chance and culture. Example:Regex.Split("FirstStopSecondSTOPThird", "stop", RegexOptions.IgnoreCase)

will result in:

First
Second
Thrid
+4
source

String.Split() - ?

. . , , , . , , , X X? , .NET , .

?

, . . - , :

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();
}
+5

Source: https://habr.com/ru/post/1608470/


All Articles