【发布时间】:2016-01-19 14:37:35
【问题描述】:
我有长度超过 150 个字符的字符串。我想删除前 17 个字符并保留其余字符。我在 ASP.net 4.5 中使用 Substring 方法,但在 str0 处收到错误消息:“System.ArgumentOutOfRangeException”。
public static string extract(string dirinfo)
{
Int32 lensub = Convert.ToInt32(dirinfo.Length);
string str0 = dirinfo.Substring(17, lensub);
return str0;
}
【问题讨论】:
-
这是因为
Substring(17, lensub)表示从第 17 个字符开始,然后向前移动与字符串全长相同的空格数。显然,从最后 17 个字符开始,您现在“超出范围”。试试Substring(17, lensub - 17);
标签: c# asp.net string substring asp.net-4.5