Web Develop Wolf
Web Develop Wolf

Reputation: 6306

Additional information: Index and length must refer to a location within the string

I have a substring which is defined as:

<p>@Html.Raw(item.Post.Substring(0, 300))...</p>

The text I'm entering far exceeds 300 characters, but I'm still getting the error above. I thought the second param on the substring method was supposed to be the number of characters you want to cut to?

Unless I'm wrong and I'm missing something?

Upvotes: 1

Views: 352

Answers (1)

tchelidze
tchelidze

Reputation: 8308

Try following

item.Post.Substring(0, Math.Min(300, item.Post.Length));

Upvotes: 1

Related Questions