Index and length should relate to the location inside the string?

I am trying to get the first 50 letters, so I used the subString function to get it.

As you can see, I used this code to get it:

 <%# Eval("BannerDescription").ToString.Substring(1, 50)%> 

But, unfortunately, it does not work and an error message appears:

The index and length must refer to the location within the string.

So is there any other way to fix it?

since the user controls the data entry! several times he was going to enter 10 letters at another time, maybe 1000 letters, so how can I solve this?

I tried them all, but we can use it like this:

<%# IIf(Eval("BannerDescription").ToString().Length > 49, Eval("BannerDescription").ToString().Substring(0, 49), Eval("BannerDescription"))%>

Thanks.

+7
source share
7 answers

Maybe something like this -

 <%# Eval("BannerDescription").ToString().Substring(0, Math.Min(Eval("BannerDescription").ToString().Length, 50)) %> 
+18
source
 <%# new string(Eval("BannerDescription").ToString().Take(50).ToArray()) %> 
+3
source

You should be able to implement a function that will give you a shorter version if it is too long:

 <% Function ShortVersion (strData, strLen) If Len (strData) > strLen Then ShortVersion = Left (strData, strLen) Else ShortVersion = strData End If End Function %> 
+1
source

NOTE: This answer is for since this question was asked when this answer was sent.


This will give you no more than 50 characters:

 <%# Eval("BannerDescription").ToString().Length > 50 ? Eval("BannerDescription").ToString().Substring(0, 50) : Eval("BannerDescription").ToString() %> 

Even better, enter a secure method in the back code (.cs), which can be referenced in the markup:

Markup:

 <%# GetStringMaxLength(Eval("BannerDescription").ToString(), 50) %> 

Code for:

 protected static string GetStringMaxLength(string text, int maxLength) { if (string.IsNullOrEmpty(text)) { return string.Empty; } if (text.Length > maxLength) { return text.Substring(0, maxLength); } return text; } 

So this is a cleaner look. :-)

+1
source

If this is your problem and you are using .NET 3.5, you can try:

 public static class StringEx { public static string SubstringNE(this string str, int index, int length) { if (index >= str.Length) { return String.Empty; } if (index + length > str.Length) { return str.Substring(index); } return str.Substring(index, length); } } 

You call it the same as Substring , but it will not throw the wrong index / length. Example: <%# Eval("BannerDescription").ToString().SubstringNE(1, 50)%> . NE for "Without exception."

+1
source

50 is more than the length of your result string, your code will fail in all cases when it is true. You must not specify a parameter that exceeds the number of characters in it. Use the String.Length property, which will give you the dynamic number of characters in String. Therefore, if your string is longer than 50 characters (or equal to 50), you can say that you get the first 50 char, otherwise you can only get the whole string, which is slightly less than 50 characters.

You can use this code:

Eval ("BannerDescription"). ToString (). Length> 49? Eval ("BannerDescription"). ToString (). SubString (0, 49): Eval ("BannerDescription"). Tostring ()

Regards, Peter

0
source

Use the code snippet below, I have used it many times in my projects.

 <%# Eval("BannerDescription").ToString().Length <= 22 ? Eval("BannerDescription") : string.Format("{0}....", Eval("BannerDescription").ToString().Substring(0,22))%> 
0
source

All Articles