下面代码在 int li_str = Convert.ToInt16(ls_str);
会报 “输入字符串的格式不正确。”的错误string[] index_temp;
string indexStr="24503;22269;19993;32452;32852;36187;";
index_temp = indexStr.Split(';');
indexStr = "";
for (int j = 0; j < index_temp.Length; j++)
{
if (index_temp[j] != "")
{
string ls_str = index_temp[j];
int li_str = Convert.ToInt16(ls_str);
char lc_str = (char)li_str; indexStr = indexStr + lc_str.ToString();
}
}
会报 “输入字符串的格式不正确。”的错误string[] index_temp;
string indexStr="24503;22269;19993;32452;32852;36187;";
index_temp = indexStr.Split(';');
indexStr = "";
for (int j = 0; j < index_temp.Length; j++)
{
if (index_temp[j] != "")
{
string ls_str = index_temp[j];
int li_str = Convert.ToInt16(ls_str);
char lc_str = (char)li_str; indexStr = indexStr + lc_str.ToString();
}
}
string[] index_temp;
string indexStr = "24503;22269;19993;32452;32852;36187;";
index_temp = indexStr.Split(';');
indexStr = "";
for (int j = 0; j < index_temp.Length; j++)
{
if (index_temp[j] != "")
{
string ls_str = index_temp[j];
int li_str = int.Parse(ls_str);
char lc_str = (char)li_str; indexStr = indexStr + lc_str.ToString();
}
}
Console.Write(indexStr);
Console.ReadLine();
string[] index_temp;
string indexStr = "24503;22269;19993;32452;32852;36187;";
index_temp = indexStr.Split(';');
indexStr = "";
for (int j = 0; j < index_temp.Length; j++)
{
if (index_temp[j] != "")
{
string ls_str = index_temp[j];
Int32 li_str = Convert.ToInt32(ls_str);
char lc_str = (char)li_str; indexStr = indexStr + lc_str.ToString();
}
}数值超过int16的值 用int32
应改为:
Int32 li_str = Convert.ToInt32(ls_str);
int li_str = Convert.ToInt32(ls_str);
你那个Convert.ToInt16长度太短了溢出了!
按‘;’分割,数组的最后一个为空,所以转化为int时报错。
List<char> c = new List<char>();
foreach (string str in indexStr.Split(';'))
{
if (!string.IsNullOrEmpty(str))
c.Add(Convert.ToChar(Convert.ToInt32(str)));
}
indexStr = new string(c.ToArray());
引用 11 楼 xin_shui 的回复:
string indexStr="24503;22269;19993;32452;32852;36187;";
按‘;’分割,数组的最后一个为空,所以转化为int时报错。
这个是正解 这个就是你代码报错的根本原因这个是正解