html.encode上的子字符串(substring on html.encode)

编程入门 行业动态 更新时间:2024-10-18 22:30:27
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))%>

更多推荐

本文发布于:2023-08-05 20:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1437991.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   html   encode   substring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!