Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 247
Mario Avatar asked Jan 21 '13 22:01

Mario


3 Answers

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)
like image 127
marapet Avatar answered Nov 09 '22 10:11

marapet


Use It (:

< % # Eval("MyText").ToString().Length <= 30 ? Eval("MyText") : Eval("MyText").ToString().Substring(0, 30)+"..." % > 
like image 4
Serdar KUŞ Avatar answered Nov 09 '22 10:11

Serdar KUŞ


Damn I like LINQ:

string.Concat('<%# Eval("My Text") %>'.ToString().Where((char, index) => index < 140))
like image 3
gdoron is supporting Monica Avatar answered Nov 09 '22 09:11

gdoron is supporting Monica