How to limit text string in Eval

2019-02-21 17:38发布

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.

5条回答
冷血范
2楼-- · 2019-02-21 17:49

Damn I like LINQ:

string.Concat('<%# Eval("My Text") %>'.ToString().Where((char, index) => index < 140))
查看更多
做自己的国王
3楼-- · 2019-02-21 17:54

Use It (:

< % # Eval("MyText").ToString().Length <= 30 ? Eval("MyText") : Eval("MyText").ToString().Substring(0, 30)+"..." % > 
查看更多
三岁会撩人
4楼-- · 2019-02-21 17:56

Similar to Leniel's answer but with a twist.... Sometimes I like to append an ellipsis to demonstrate the displayed string has been truncated.

    /// <summary>
    /// Converts the value of the specified string to a truncated string representation
    /// </summary>
    /// <param name="source">The specified string</param>
    /// <param name="length">Integer specifying the maximum number of characters to retain from the specified string.</param>
    /// <param name="appendEllipsis">Determines whether or not to append an ellipsis to the truncated result.  If the specified string is shorter than the length parameter the ellipsis will not be appended in any event.</param>
    /// <returns>A truncated string representation of the specified string.</returns>
    public static String Truncate(this String source, int length, bool appendEllipsis = false)
    {
        if (source.Length <= length)
            return source;

        return (appendEllipsis)
            ? String.Concat(source.Substring(0, length), "...")
            : source.Substring(0, length);
    }
查看更多
再贱就再见
5楼-- · 2019-02-21 17:57

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)
查看更多
贼婆χ
6楼-- · 2019-02-21 18:02

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
查看更多
登录 后发表回答