How to limit text string in Eval

不羁的心 提交于 2019-12-01 15:22:14

问题


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.


回答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)



回答2:


Use It (:

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



回答3:


Damn I like LINQ:

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



回答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



回答5:


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);
    }


来源:https://stackoverflow.com/questions/14448511/how-to-limit-text-string-in-eval

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