<%# GetString(DataBinder.Eval(Container.DataItem,"intro").ToString().Replace(".","").Replace("<BR>","").Replace(" ","").Replace("<","").Replace(">",""),17) %>public string GetString(String s, int n)
{
byte[] b1 = System.Text.Encoding.Default.GetBytes(s);
if (b1.Length > n)
return s.Substring(0,n) + "..."; return s;
}报错 System.ArgumentOutOfRangeException: Index and length must refer to a location within the string. Parameter name: length记录有点多,13万多条。具体是哪条传进来出问题不好说。
帮忙分析一下是哪的问题。比如非法字符?长度? 如何完善GetString?
{
byte[] b1 = System.Text.Encoding.Default.GetBytes(s);
if (b1.Length > n)
return s.Substring(0,n) + "..."; return s;
}报错 System.ArgumentOutOfRangeException: Index and length must refer to a location within the string. Parameter name: length记录有点多,13万多条。具体是哪条传进来出问题不好说。
帮忙分析一下是哪的问题。比如非法字符?长度? 如何完善GetString?
服务器上记录多。 不知道哪些字符串传进来会报这个错?
/// <summary>
/// 获取字符串的实际长度(一个中文字符按两个单位计算)
/// </summary>
/// <param name="str">原字符串</param>
public static int Length(string str)
{
int result = 0;
foreach(char chr in str)
{
if((int)chr > 127)
{
result += 2;
}
else
{
result ++;
}
}
return result;
}
/// <summary>
/// 按字符串实际长度截取定长字符窜
/// </summary>
/// <param name="str">原字符串</param>
/// <param name="length">要截取的长度</param>
public static string Substring(string str, int length)
{
int i = 0, j = 0;
foreach(char chr in str)
{
if((int)chr > 127)
{
i += 2;
}
else
{
i ++;
}
if (i > length)
{
str = str.Substring(0, j) + "...";
break;
}
j ++;
}
return str;
}