Split - Index was outside the bounds of the array

后端 未结 7 577
野性不改
野性不改 2020-12-18 04:25

Im using the following code to split up a string and store it:

string[] proxyAdrs = linesProxy[i].Split(\':\');
string proxyServer = proxyAdrs[0];
int proxyP         


        
7条回答
  •  猫巷女王i
    2020-12-18 05:08

    Just check the length of the array returned in your if statement

    if( proxyAdrs.Length > 2 &&  proxyAdrs[2] != null)
        {
            item.Username = proxyAdrs[2];
        }
    

    The reason you are getting the exception is that the split is returning array of size less than the index you are accessing with. If you are accessing the array element 2 then there must be atleast 3 elements in the array as array index starts with 0

提交回复
热议问题