请问这些代码有什么不妥的地方吗?使用过程中有时会出错,提示
索引和长度必须引用该字符串内的位置。
参数名: length
异常详细信息: System.ArgumentOutOfRangeException: 索引和长度必须引用该字符串内的位置。
参数名: length源错误:
行 221: str = str.Replace("<br />", "\n");
行 222: str = str.Replace("<br/>", "\n");
行 223: string temp = str.Substring(0, end);
行 224:
行 225: if (str.Length > end)以下是代码
----------------------------------------------------------------------------------
public static string leftstr(string str, int len)
{
int intlen = str.Length;
int start = 0;
int end = intlen;
int single = 0;
char[] chars = str.ToCharArray();
for (int i = 0; i < chars.Length; i++)
{
if (Convert.ToInt32(chars[i]) > 255)
{
start += 2;
}
else
{
start += 1;
single++;
}
if (start >= len)
{
if (end % 2 == 0)
{
if (single % 2 == 0)
{
end = i + 1;
}
else
{
end = i;
}
}
else
{
end = i + 1;
}
break;
}
}
str = str.Replace("<br>", "\n");
str = str.Replace("<br />", "\n");
str = str.Replace("<br/>", "\n");
string temp = str.Substring(0, end); if (str.Length > end)
{
return temp + "…";
}
else
{
return temp;
}
}
索引和长度必须引用该字符串内的位置。
参数名: length
异常详细信息: System.ArgumentOutOfRangeException: 索引和长度必须引用该字符串内的位置。
参数名: length源错误:
行 221: str = str.Replace("<br />", "\n");
行 222: str = str.Replace("<br/>", "\n");
行 223: string temp = str.Substring(0, end);
行 224:
行 225: if (str.Length > end)以下是代码
----------------------------------------------------------------------------------
public static string leftstr(string str, int len)
{
int intlen = str.Length;
int start = 0;
int end = intlen;
int single = 0;
char[] chars = str.ToCharArray();
for (int i = 0; i < chars.Length; i++)
{
if (Convert.ToInt32(chars[i]) > 255)
{
start += 2;
}
else
{
start += 1;
single++;
}
if (start >= len)
{
if (end % 2 == 0)
{
if (single % 2 == 0)
{
end = i + 1;
}
else
{
end = i;
}
}
else
{
end = i + 1;
}
break;
}
}
str = str.Replace("<br>", "\n");
str = str.Replace("<br />", "\n");
str = str.Replace("<br/>", "\n");
string temp = str.Substring(0, end); if (str.Length > end)
{
return temp + "…";
}
else
{
return temp;
}
}
解决方案 »
- 一个纠结了很久的问题。
- 求助:windows server 2008下的发布问题
- 一个很奇怪的问题,GridView修改出现文本框
- membership 问题
- 一个函数的问题,很有意思的阿!
- 文件上传问题,好奇怪,救命啊~~~
- 《〈〈〈〈帮忙看看这个问题错在那里
- freetextbox配置和使用方法祥解
- 根据用户IP找到离分公司最近的分公司
- 我在DATAGRID中做了一个分页,第一次查询时分了10页,翻到第10页,第二次查询时小于10页就报错了。Invalid CurrentPageIndex value. It m
- 【第二问`初学者求助】JS源码读不懂 求解释
- image 路径问题,进来看下!·······
加了这句好象就没事了
if(str.Length>end)
string temp = str.Substring(0, end);