Search code examples
c#asp.netstringevaltruncate

How to limit text string in Eval


I have a hyperlink with the navigate property set like this:

NavigateUrl='<%# Eval("My Text") %>'

How can I limit the string to 140 characters ? I have tried this Eval("My Text").ToString().Substring(0,140) but if the string length is less than 140 characters it throws an exception.


Solution

  • And yet an other possibility:

    Eval("My Text").ToString().PadRight(140).Substring(0,140).TrimEnd()
    

    Edit:

    I do like LINQ, too:

    Eval("My Text").ToString().Take(140).Aggregate("", (x,y) => x + y)