Vb.net 数组或列表中的项目是否保持其顺序?
我正在VS2008中编写VB.NET 我有一个逗号分隔的数字字符串,即16,7,991456,1,3 我在VB中执行此操作:Vb.net 数组或列表中的项目是否保持其顺序?,vb.net,arrays,string,sorting,Vb.net,Arrays,String,Sorting,我正在VS2008中编写VB.NET 我有一个逗号分隔的数字字符串,即16,7,991456,1,3 我在VB中执行此操作: Dim MyArr() As String = MyString.Split(",") MyArr是否会按项目在字符串中的顺序保留项目 如果我这样做: For Each S as String in MyString.Split(",") 'Do something with S 'Will my items be in the same order t
Dim MyArr() As String = MyString.Split(",")
MyArr是否会按项目在字符串中的顺序保留项目
如果我这样做:
For Each S as String in MyString.Split(",")
'Do something with S
'Will my items be in the same order they were
'in the string?
Next
我对它进行了测试,它似乎保持了排序顺序,但它会~始终~保持顺序吗
如果它不能维持顺序,那么分割字符串并保持顺序的好方法是什么
我之所以这么问,是因为MSDN数组文档中说:“数组不能保证排序。”所以我有点不确定。是的,在您的示例中,项目将保持原始顺序
MSDN文档指出,数组不一定因为是数组而被排序,但一旦项目在数组中,它们就不会被重新排列。Split()操作将根据给定的标记将其分解,并保留顺序。是的,将为这些操作保留顺序。在.NET中,字符串是。长话短说,字符串S和Split(“,”)返回的字符串位于不同的内存中。是的,字符串。Split沿着字符串走,所有内容都将保持有序。从.NET Reflector:
private string[] InternalSplitKeepEmptyEntries(int[] sepList, int[] lengthList, int numReplaces, int count)
{
int startIndex = 0;
int index = 0;
count--;
int num3 = (numReplaces < count) ? numReplaces : count;
string[] strArray = new string[num3 + 1];
for (int i = 0; (i < num3) && (startIndex < this.Length); i++)
{
strArray[index++] = this.Substring(startIndex, sepList[i] - startIndex);
startIndex = sepList[i] + ((lengthList == null) ? 1 : lengthList[i]);
}
if ((startIndex < this.Length) && (num3 >= 0))
{
strArray[index] = this.Substring(startIndex);
return strArray;
}
if (index == num3)
{
strArray[index] = Empty;
}
return strArray;
}
private string[]internalsplitkeepmptyentries(int[]sepList,int[]lengthList,int numReplaces,int count)
{
int startIndex=0;
int指数=0;
计数--;
int num3=(numReplaces<计数)?numReplaces:计数;
字符串[]strArray=新字符串[num3+1];
对于(int i=0;(i=0))
{
strArray[index]=这个.Substring(startIndex);
回程线;
}
如果(指数=num3)
{
strArray[索引]=空;
}
回程线;
}
我有点误解了这个问题。塞尔盖有一个更清晰更精确的答案,是的。我认为“保存”是需要记住的关键问题。这就是我想要的澄清。谢谢