如何在Eval中限制文本字符串

问题描述 投票:9回答:6

我有一个超链接,其导航属性设置如下:

NavigateUrl='<%# Eval("My Text") %>'

如何将字符串限制为140个字符?我已经尝试过此Eval(“ My Text”)。ToString()。Substring(0,140),但是如果字符串长度小于140个字符,则会引发异常。

c# asp.net string eval truncate
6个回答
18
投票

还有另一种可能性:

Eval("My Text").ToString().PadRight(140).Substring(0,140).TrimEnd()

编辑:

我也喜欢LINQ:

Eval("My Text").ToString().Take(140).Aggregate("", (x,y) => x + y)

4
投票

使用它(:

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

3
投票

该死,我喜欢LINQ:

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

2
投票

您可以尝试如下所示的Truncate方法:

C# Truncate String

只需在源参数之前添加this关键字,即可将其转换为扩展方法。这是一种比较复杂的方法,但是在需要在其他地方重用的情况下可能是有价值的...

就您而言,您将:

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

完整的控制台测试应用程序:

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

输出:

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

0
投票

与Leniel的答案类似,但有一个转折。...有时我想附加一个省略号以说明显示的字符串已被截断。

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

0
投票

这对我有用-truncate()函数没有在我的Visual Studio中上拉,所以我使用了Substring()

Text='<%# Eval("LastChangedTime", "{0:t}").ToString().Substring(0,10) %>'

这将显示时间到最接近的1/10秒。

© www.soinside.com 2019 - 2024. All rights reserved.