String.Split方法确保结果数组中的顺序吗?

ibu*_*kov 7 .net c# string split

流利的谷歌搜索没有给出答案,所以问题是:

String.Split方法是否根据它们在初始字符串中的位置确保生成的子串的顺序?

谢谢.

Uwe*_*eim 9

根据ILSpy在内部显示的内容string.Split,答案是肯定的.

private string[] InternalSplitKeepEmptyEntries(
    int[] sepList, int[] lengthList, int numReplaces, int count)
{
    int num = 0;
    int num2 = 0;
    count--;
    int num3 = (numReplaces < count) ? numReplaces : count;
    string[] array = new string[num3 + 1];
    int num4 = 0;
    while (num4 < num3 && num < this.Length)
    {
        array[num2++] = this.Substring(num, sepList[num4] - num);
        num = sepList[num4] + ((lengthList == null) ? 1 : lengthList[num4]);
        num4++;
    }
    if (num < this.Length && num3 >= 0)
    {
        array[num2] = this.Substring(num);
    }
    else
    {
        if (num2 == num3)
        {
            array[num2] = string.Empty;
        }
    }
    return array;
}
Run Code Online (Sandbox Code Playgroud)

所有元素(例如array变量)总是按升序处理,不进行排序.

为MSDN文档string.Split还列出了必须在相同的顺序为它们在原始字符串命令结果的例子.

正如Jim Mischel上面指出的那样,这只是当前的实施,可能会改变.

  • 坚实的证据.还有很好的速度. (4认同)