"2003-3"分解成"2003"和"3"写成如下形式出错!
s_time.Substring(0,s_time.IndexOf("-",0,s_time.Length));
s_time.Substring(s_time.IndexOf("-",0,s_time.Length) + 1,s_time.Length - s_time.IndexOf("-",0,s_time.Length));
各位指点一下!!!
谢谢!!!
你可以这样写,也可以分割:
string s_time = "2003-3";
string[] StrTemp = s_time.Split(-);
string str="2003-3";
char[] Sp = new char[1];
Sp[0]=-;
string[] arry=str.Split(Sp);
Response.Write(arry[0].ToString()); //"2003"
Response.Write(arry[1].ToString()); //"3"
哦,写错了
s_time.Remove(0,s_time.LastIndexOf("-")+1);取之后
s_time.Substring(0,s_time.IndexOf("-"));取之前
正确代码如下:
string s_time="2003-5";
//串的序是从0~Length-1,当序到了Length时已越界!
//得到“2003”
Label1.Text=s_time.Substring(0,s_time.IndexOf("-",0,s_time.Length-1));
//得到“5”
Label2.Text=s_time.Substring(s_time.IndexOf("-",0,s_time.Length-1) + 1,s_time.Length-1 - s_time.IndexOf("-",0,s_time.Length-1));
仔细阅读一下系统的错误提示,会对你分析调试程序有帮助的!
另外,用Substring()确实较繁,建议用Split()简洁明了得多。 ^-^