9

我有一个带有这样设置的导航属性的超链接:

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

如何将字符串限制为 140 个字符?我试过这个 Eval("My Text").ToString().Substring(0,140) 但如果字符串长度小于 140 个字符,则会引发异常。

4

6 回答 6

19

还有另一种可能性:

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

编辑:

我也喜欢 LINQ:

Eval("My Text").ToString().Take(140).Aggregate("", (x,y) => x + y)
于 2013-01-21T22:43:55.060 回答
4

用它 (:

< % # Eval("MyText").ToString().Length <= 30 ? Eval("MyText") : Eval("MyText").ToString().Substring(0, 30)+"..." % > 
于 2013-06-04T08:48:41.533 回答
3

该死的我喜欢LINQ:

string.Concat('<%# Eval("My Text") %>'.ToString().Where((char, index) => index < 140))
于 2013-01-21T22:46:29.800 回答
2

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

C# 截断字符串

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
于 2013-01-21T22:42:40.370 回答
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);
    }
于 2014-05-01T12:19:16.143 回答
0

这对我有用 - 该truncate()功能没有在我的 Visual Studio 中启动,所以我使用了Substring().

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

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

于 2020-04-20T11:13:28.333 回答