html.encode上的子字符串(substring on html.encode)
如何限制为html.encode显示的字符数?
<%= Html.Encode(item.LastName.Substring(1,30))%>错误:索引和长度必须引用字符串中的位置。
how can I limit the # of characters to display for html.encode?
<%= Html.Encode(item.LastName.Substring(1,30))%>error: Index and length must refer to a location within the string.
最满意答案
您需要检查字符串长度是否大于30,否则您指定的长度将从字符串的末尾开始...(我也将您的起始索引更改为0,假设您不想遗漏第一个字符)
<%= Html.Encode(item.LastName.Substring(0, item.LastName.Length > 30 ? 30 : item.LastName.Length))%>You need to check the strings length is greater than 30, otherwise you are specifying a length which will fall off the end of the string...(I've also changed your start index to 0 assuming you didn't mean to leave out the first character)
<%= Html.Encode(item.LastName.Substring(0, item.LastName.Length > 30 ? 30 : item.LastName.Length))%>更多推荐
发布评论