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.
Damn I like LINQ:
Use It (:
Similar to Leniel's answer but with a twist.... Sometimes I like to append an ellipsis to demonstrate the displayed string has been truncated.
And yet an other possibility:
Edit:
I do like LINQ, too:
You can try the Truncate method as shown here:
C# Truncate String
Convert it to an extension method by simply adding the
this
keyword before the source parameter. It's a more convoluted approach but may be of value in cases where you need to reuse it somewhere else...In your case, you'd have:
Complete console test app:
Output: