How to limit text string in Eval

18,812

Solution 1

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)

Solution 2

Use It (:

< % # Eval("MyText").ToString().Length <= 30 ? Eval("MyText") : Eval("MyText").ToString().Substring(0, 30)+"..." % > 

Solution 3

Damn I like LINQ:

string.Concat('<%# Eval("My Text") %>'.ToString().Where((char, index) => index < 140))

Solution 4

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:

NavigateUrl='<%# Eval("My Text").ToString().Truncate(140) %>'

Complete console test app:

using System;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            string test1 = "A really big string that has more than 140 chars. This string is supposed to be trunctaded by the Truncate extension method defined in class StringTool.";

            Console.WriteLine(test1.Truncate(140));

            Console.ReadLine();
        }
    }

    /// <summary>
    /// Custom string utility methods.
    /// </summary>
    public static class StringTool
    {
        /// <summary>
        /// Get a substring of the first N characters.
        /// </summary>
        public static string Truncate(this string source, int length)
        {
            if (source.Length > length)
            {
                source = source.Substring(0, length);
            }
            return source;
        }

        /// <summary>
        /// Get a substring of the first N characters. [Slow]
        /// </summary>
        public static string Truncate2(this string source, int length)
        {
            return source.Substring(0, Math.Min(length, source.Length));
        }
    }
}

Output:

A really big string that has more than 140 chars. This string is supposed to be
trunctaded by the Truncate extension method defined in class
Share:
18,812
Mario
Author by

Mario

Updated on June 04, 2022

Comments

  • Mario
    Mario almost 2 years

    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.

    • Mike Christensen
      Mike Christensen over 11 years
      Maybe write an extension method?
  • Aristos
    Aristos over 11 years
    I like also what you have type - but how about the speed of it ? Isn't too much for a simple string length cut (and also slow ?)
  • gdoron is supporting Monica
    gdoron is supporting Monica over 11 years
  • Aristos
    Aristos over 11 years
    Yes, Which is faster ? for sure the linq compile over a table with 20 lines is slower, because if its not static, then is compile 20 times to know that is need to just cut a string.
  • Aristos
    Aristos over 11 years
    To avoid an if (lenght > 140) you use more memory and time. :)
  • gdoron is supporting Monica
    gdoron is supporting Monica over 11 years
    @Aristos, the goal of the article which I hope you will read is: Do you really care? In this case I believe you don't, like in 99.99% of times.
  • Aristos
    Aristos over 11 years
    In talks everything sound philosophical nice - but I care about speed, and I care to make fast programs, I like to work with fast programs. linq is slow, make too many compiles in real time - if you use it too much, you end up with a slow program. :)
  • gdoron is supporting Monica
    gdoron is supporting Monica over 11 years
    @Aristos, then stop using LINQ, LINQ is the devil itself, the root of all slowness-LINQ. happy? :)
  • Aristos
    Aristos over 11 years
    :) you are sentimental :) nothing to do with real arguments... :) anyway. I say to you one more time - you have a linq inside a loop, that makes its much slow for a simple basic string cut. (its inside a loop because is use the Eval that declare that get the data from a database return.)
  • Justin German
    Justin German over 11 years
    @Aristos I know, especially so if length is less than 140. But what's even worse: If the original string ("My Text") is less than 140 characters long and contains trailing spaces, those trailing spaces get chopped off. I wouldn't recommend this solution if those spaces matter and if it were to be used everywhere and every bit of performance counts. Then again, as mentioned, it's just "yet an other possibility". And it's the shortest one on this page :-)
  • Justin German
    Justin German over 11 years
    @gdoron Are you sure this is working? In think Join needs a separator, and not sure about char being a valid name for the parameter. And last but not least, the OP seems to want to use it in a web form, so you probably can't wrap a string.Join around the eval statement (though not 100% sure about that...). I added a LINQ variant without string.Join to my answer.
  • Aristos
    Aristos over 11 years
    Just for play I make a speed test, your first function takes 20ms, your second 600ms, and Leniel takes 5ms (on 50000 compares);
  • gdoron is supporting Monica
    gdoron is supporting Monica over 11 years
    @marapet, D'oh! I meant string.Concat not string.join, you're right. Fixed! And last but least, you can use LINQ and other methods with it.
  • gdoron is supporting Monica
    gdoron is supporting Monica over 11 years
    Come on, on 50000 compares you got 580ms difference? It's nothing!!! If you care so much about performance start using c or even Assembly. C# isn't for micro-optimization. Period. my +1.
  • Ahmad Z. Tibi
    Ahmad Z. Tibi over 10 years
    Is there way to avoid trim word in the middle, and trim before or after it?
  • Justin German
    Justin German over 10 years
    @AhmedYazanTibi I don't know what you mean by trim word in the middle. You may consider creating a new question with more details and examples.