How to limit text string in Eval

倖福魔咒の 提交于 2019-12-01 17:00:00

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)

Use It (:

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

Damn I like LINQ:

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

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

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);
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!