c# - String.Split 方法是否确保结果数组中的顺序?

标签 c# .net string split

Fluent google 没有给出答案,所以问题是:

String.Split 方法是否确保结果子字符串的顺序符合它们在初始字符串中的位置?

最佳答案

根据什么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;
}

所有元素(例如 array 变量)始终按升序处理,不进行排序。

MSDN documentation for string.Split还列出了结果与原始字符串中的顺序相同的示例。

作为Jim Mischel上面指出,这只是当前的实现,可能会发生变化。

关于c# - String.Split 方法是否确保结果数组中的顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8664622/

相关文章:

c# - 从文件中删除包含多个字符串的行的最有效方法?

c# - 缩小 MySQL 查询的结果

c# - 在我的 BIOS 设置中没有启用 Hyper-V 的选项

c# - C# : How to ensure an object instance can only be created by a factory class? 中的工厂模式

c# - Coverity、Enumerable.Where(this ...) 和 IDisposable

c# - JsonDiffpatch().Diff() 无法比较电子邮件地址和长度超过 50 个字符的字符串?

Java String ReplaceAll 方法对循环有异常影响?

c# - 如何为此代码编写 LINQ to objects 等效代码

java - 如何在读取文件时跳过空行(java)?

将字符串中的一个字符更改为字符串