首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

split分割,然后存入数组的有关问题!

2012-03-02 
split分割,然后存入数组的问题!!!隐藏空间b的值是若干个以逗号间隔的,我想用split分割逗号,然后把每个值存

split分割,然后存入数组的问题!!!
隐藏空间b的值是若干个以逗号间隔的,我想用split分割逗号,然后把每个值存到数组里面。我的代码是这样的,但有错误,数组索引超出上限了。。。
string   str=b.Value;
string[]   abc=str.Split( ', ');

this.hi1.Value=abc[0].ToString();
this.hi2.Value=abc[1].ToString();
this.hi3.Value=abc[2].ToString();

this.Image1.ImageUrl=this.hi1.Value;
this.Image2.ImageUrl=this.hi2.Value;
this.Image3.ImageUrl=this.hi3.Value;

如果一加上第三个,就会报错,不加则没错误。。。

如何解决呢

[解决办法]
b.Value 里面只有一个逗号就会这样了,你看看b的内容是啥就知道了
[解决办法]
if (str.Substring(str.Length - 1, 1) == ", ")
{
str = str.Remove(str.Length - 1);
}
不是给你打了一遍么
[解决办法]
//从隐藏域hinWorkDetail中获取所有值
string strWo=hinWorkDetail.Value;
int[] intW=new int[5];
string[] strW=strWo.Split( ', ');
int intLen2=strW.Length-1;
switch(intLen2)
{
case 1:
intW[0]=int.Parse(strW[0]);
break;
case 2:
intW[0]=int.Parse(strW[0]);
intW[1]=int.Parse(strW[1]);
break;
case 3:
intW[0]=int.Parse(strW[0]);
intW[1]=int.Parse(strW[1]);
intW[2]=int.Parse(strW[2]);
break;
case 4:
intW[0]=int.Parse(strW[0]);
intW[1]=int.Parse(strW[1]);
intW[2]=int.Parse(strW[2]);
intW[3]=int.Parse(strW[3]);
break;
default:
intW[0]=int.Parse(strW[0]);
intW[1]=int.Parse(strW[1]);
intW[2]=int.Parse(strW[2]);
intW[3]=int.Parse(strW[3]);
intW[4]=int.Parse(strW[4]);
break;
}

热点排行