![](/img/trans.png)
[英]The splitting of a string without using String.Split method does not return correct result
[英]Does String.Split method ensure order in result array?
流利的谷歌搜索沒有給出答案,所以問題是:
String.Split
方法是否根據它們在初始字符串中的位置確保生成的子串的順序?
根據ILSpy在string.Split的內部顯示的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
變量)始終按升序處理,不進行排序。
string.Split
的MSDN文檔還列出了示例,其結果與原始字符串中的順序相同。
正如Jim Mischel上面指出的那樣,這只是當前的實施,可能會改變。
是的。 否則它將毫無用處。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.