Split - Index was outside the bounds of the array

2020-02-06 03:14发布

问题:

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

string[] proxyAdrs = linesProxy[i].Split(':');
string proxyServer = proxyAdrs[0];
int proxyPort = Convert.ToInt32(proxyAdrs[1]);


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

if (proxyAdrs[3] != null)
{
    item.Password = proxyAdrs[3];
}

The problem is i am getting

Index was outside the bounds of the array.

When proxyAdrs[2] is not there.

Sometimes proxyAdrs[2] will be there sometimes not.

How can i solve this?

回答1:

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



回答2:

You can check the length of array before accessing its element by index.

Change

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

To

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


回答3:

Try this:

        string[] proxyAdrs = linesProxy[i].Split(':');
        string proxyServer = proxyAdrs[0];
        int proxyPort = Convert.ToInt32(proxyAdrs[1]);


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

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


回答4:

Check the length of proxyAdrs before you attempt to subscript a potentially non-existent item.

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


回答5:

It is that your i which might be lower than the 2 you are trying to set in as index :)

if i >= 2 then you can do all of the follwing:----

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

        if (proxyAdrs[3] != null)
        {
            item.Password = proxyAdrs[3];
        }
}
else I suggest you get out :D

But again, checking proxyAdrs.Lenght would be the best.



回答6:

There are two options that may help you, depending of whether or not you form incoming data (variable linesProxy):

  1. If you do form incoming data: Always include all parts of the string. In your case, always ensure you have 4 (assuming proxyAdrs[3] is the last) parts by adding additional : between 1st and 3rd values if no value for 2nd is provided. Thus after .Split() operation (ensure you don't activate RemoveEmptyStrings option ) your proxyAdrs[2] will be null and your sample will be fine.
  2. Otherwise: if proxyAdrs[2] is the only part the can be empty following snippet can prevent crashing:

    string[] proxyAdrs = linesProxy[i].Split(':');
    string proxyServer = proxyAdrs[0];
    int proxyPort = Convert.ToInt32(proxyAdrs[1]);    
    
    if(proxyAdrs.Length > 3)
    {
      if(proxyAdrs[2] != null)
          item.Username = proxyAdrs[2];
      if (proxyAdrs[3] != null)
          item.Password = proxyAdrs[3];
    }
    else
    {
     if(proxyAdrs[2] != null)
          item.Password = proxyAdrs[2];
    }
    


回答7:

try
{
    objCommonDD = new CommonDropDownBLL();
    objCommonDDEntity = new CommonDropdownEntity();

    //string strState=contextKey.ToString();
    string[] contextKeySplit = contextKey.Split('^');
    string strState = contextKeySplit[0].ToString();
    string strPin = contextKeySplit[1].ToString();

    objCommonDDEntity.TableName = "PCOM_PINCODES";
    objCommonDDEntity.DeleteField = "";
    objCommonDDEntity.TextField = "RTRIM(PIN_CITY_NAME) AS PC_DESC";
    objCommonDDEntity.ValueField = "DISTINCT PIN_CITY_CODE AS PC_CODE";

    objCommonDDEntity.StrCondition = " AND PIN_COUNTRY_CODE='IND' AND UPPER(PIN_CITY_NAME) LIKE UPPER('" + prefixText + "%') AND PIN_STATE_NAME='" + strState + "' AND PIN_CODE='" + strPin + "' ORDER BY PC_DESC";

    DataTable dtCity = new DataTable();
    dtCity = objCommonDD.GetData(objCommonDDEntity);

    string[] items = new string[dtCity.Rows.Count];
    int i = 0;

    for (i = 0; i < dtCity.Rows.Count; i++)
    {
        items.SetValue(dtCity.Rows[i]["PC_DESC"].ToString(), i);
    }

    return items;
}


标签: c# split