我有點新來C#。使用分割時出現問題。我以爲它返回一個字符串數組。但一旦到達下面的最後一行,就會崩潰,並說我無法訪問它。越界。即使在拆分中它會找到多個'〜'。任何解決我的問題?問題與String.split
String tempString = " ";
while ((tempString = streamReader.ReadLine()) != null)
{
String [] split = tempString.Split('~');
typeOfVehicle = split[0];
manufactuer = split[1];
非常感謝
問題解決。
您是否檢查過'split.Length'來查看數組中有多少(以及哪些)元素? –
您將它與null比較,但不檢查它是否爲空。如果返回的最後一個字符串不能被分割,那麼它將不會有這些索引 - 嘗試使用while(!String.IsNullOrEmpty(tempString = streamReader.ReadLine()){...} –
是的數組中填充了10個項目。 – Cody