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/