문제

할 수 있는 방법을 제한하#의 문자를 표시하는 html.인코딩?

<%= Html.Encode(item.LastName.Substring(1,30))%>

오류:인덱스 및 길이의 위치를 참조해야 합니다 내의 문자열입니다.

도움이 되었습니까?

해결책

필요하신을 확인하는 문자열의 길이가 30 일,그렇지 않으면 당신이 지정하는 길이 떨어질 것이 문자열의 끝...(나 또한 변경을 시작 인덱스 0 으로 가정하지 않은 의미를 떠나 밖으로 첫 번째 문자)

<%= Html.Encode(item.LastName.Substring(0, 
                     item.LastName.Length > 30 ? 30 : item.LastName.Length))%>

다른 팁

수도 있습니다 뭔가

<%= Html.Encode(item.LastName.Substring(0, Math.Min(item.LastName.Length, 30)) %>

일부를 저장하는 바이트

<%= Html.Encode(item.LastName.Substring(0, item.LastName.Length > 30 ? 30 : item.LastName.Length))%>

를 확인하려면 null,이는 대신:

<%= Html.Encode(
item.LastName == null ? string.Empty :
item.LastName.Substring(0, item.LastName.Length > 30 ? 30 : item.LastName.Length))%>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top