C# 拆分索引超出了数组的边界
Im使用以下代码拆分字符串并存储它:C# 拆分索引超出了数组的边界,c#,split,C#,Split,Im使用以下代码拆分字符串并存储它: 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
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];
}
问题是我越来越
索引超出了数组的边界
当proxyAdrs[2]
不存在时
有时proxyard[2]会出现,有时不会
如何解决这个问题?只需检查if语句中返回的数组的长度
if( proxyAdrs.Length > 2 && proxyAdrs[2] != null)
{
item.Username = proxyAdrs[2];
}
出现异常的原因是分割返回的数组的大小小于您访问的索引。如果要访问数组元素2
,则数组中必须至少有3个元素,因为数组索引以0
开头,请尝试以下操作:
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];
}
在尝试为可能不存在的项目下标之前,请检查proxyard
的长度
if ( proxyAdrs.Length > 1 ) {
item.Username = proxyAdrs[2];
}
在按索引访问数组元素之前,可以检查数组的长度
改变
if(proxyAdrs[2] != null)
{
item.Username = proxyAdrs[2];
}
到
您的i
可能低于您试图设置为索引的2
)
如果i>=2
则可以执行以下所有操作:----
但同样,检查proxyAdrs.length
是最好的选择。根据您是否形成传入数据(变量linesProxy
),有两个选项可以帮助您:
如果要形成传入数据:请始终包含字符串的所有部分。在您的情况下,如果未提供第二个值,则始终通过在第一个值和第三个值之间添加额外的:
来确保有4个(假设proxyAdrs[3]
是最后一个)部分。因此,在执行.Split()
操作(确保未激活removemptystrings
选项)后,proxyAdrs[2]
将为null
,您的样本将正常
否则:如果proxyAdrs[2]
是唯一可以为空的部分,则以下代码段可以防止崩溃:
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];
}
试试看
{
objCommonDD=新的CommonDropDownBLL();
objcommondentity=新的CommonDropdownEntity();
//字符串strState=contextKey.ToString();
字符串[]contextKeySplit=contextKey.Split('^');
字符串strState=contextKeySplit[0].ToString();
字符串strPin=contextKeySplit[1].ToString();
objcommondentity.TableName=“PCOM_PINCODES”;
objcommondentity.DeleteField=“”;
objCommonDDEntity.TextField=“RTRIM(PIN_CITY_NAME)作为PC_DESC”;
objcommondentity.ValueField=“将PIN_城市代码与PC_代码区分开来”;
objcommondentity.StrCondition=“和PIN_COUNTRY_CODE='IND'和UPPER(PIN_CITY_NAME),如UPPER('“+prefixText+“%”)和PIN_STATE_NAME='“+strState+”,以及PIN_CODE='“+strPin+“'ORDER BY PC_DESC”;
DataTable dtCity=新DataTable();
dtCity=objCommonDD.GetData(objcommondentity);
string[]items=新字符串[dtCity.Rows.Count];
int i=0;
对于(i=0;i
欢迎来到SO deepika!这是不可接受的,所以用一个答案问另一个问题。请使用“提问”按钮提出新问题。已将其标记为删除。
if(proxyAdrs[2] != null)
{
item.Username = proxyAdrs[2];
}
if (proxyAdrs[3] != null)
{
item.Password = proxyAdrs[3];
}
}
else I suggest you get out :D
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];
}
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;
}