none
Substring RRS feed

  • Question

  • I have the following piece of code.
    <span>@Model.Interview4.Description.Substring(0, 200)</span><br />

    It works pretty well but if my Description has less than 200 characters I have an error.

    How can I fix it ?


    Friday, August 22, 2014 8:27 PM

Answers

  • Use this:

    <span>@Model.Interview4.Description.Substring(0, Math.Min(Model.Interview4.Description.Length, 200))</span><br />

    • Proposed as answer by Magnus (MM8)MVP Saturday, August 23, 2014 10:01 AM
    • Marked as answer by AGA Neto Wednesday, August 27, 2014 4:25 AM
    Friday, August 22, 2014 8:45 PM
  • Try this too:

    <span>@string.Concat(Model.Interview4.Description.Take(200))</span><br/>

    • Marked as answer by AGA Neto Wednesday, August 27, 2014 4:26 AM
    Saturday, August 23, 2014 8:02 AM

All replies

  • Use this:

    <span>@Model.Interview4.Description.Substring(0, Math.Min(Model.Interview4.Description.Length, 200))</span><br />

    • Proposed as answer by Magnus (MM8)MVP Saturday, August 23, 2014 10:01 AM
    • Marked as answer by AGA Neto Wednesday, August 27, 2014 4:25 AM
    Friday, August 22, 2014 8:45 PM
  • Try this too:

    <span>@string.Concat(Model.Interview4.Description.Take(200))</span><br/>

    • Marked as answer by AGA Neto Wednesday, August 27, 2014 4:26 AM
    Saturday, August 23, 2014 8:02 AM