C# 如何限制Eval中的文本字符串

C# 如何限制Eval中的文本字符串,c#,asp.net,string,eval,truncate,C#,Asp.net,String,Eval,Truncate,我有一个带有如下导航属性集的超链接: NavigateUrl='<%# Eval("My Text") %>' NavigateUrl='' 如何将字符串限制为140个字符? 我已尝试此Eval(“我的文本”).ToString()子字符串(0140),但如果字符串长度小于140个字符,则会引发异常。您可以尝试如下所示的Truncate方法: 只需在源参数之前添加this关键字,即可将其转换为扩展方法。这是一种更复杂的方法,但在您需要在其他地方重用它的情况下可能会很有价值 在您

我有一个带有如下导航属性集的超链接:

NavigateUrl='<%# Eval("My Text") %>'
NavigateUrl=''
如何将字符串限制为140个字符?
我已尝试此Eval(“我的文本”).ToString()子字符串(0140),但如果字符串长度小于140个字符,则会引发异常。

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

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

在您的情况下,您将有:

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

还有另一种可能性:

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

我也很喜欢林克:

Eval("My Text").ToString().Take(140).Aggregate("", (x,y) => x + y)
该死的,我喜欢林克:

string.Concat('<%# Eval("My Text") %>'.ToString().Where((char, index) => index < 140))
string.Concat(''.ToString().Where((字符,索引)=>index<140))
使用它(:

<%#Eval(“MyText”).ToString()长度

与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);
    }
//
///将指定字符串的值转换为截断的字符串表示形式
/// 
///指定的字符串
///整数,指定从指定字符串中保留的最大字符数。
///确定是否向截断的结果追加省略号。如果指定的字符串短于长度参数,则在任何事件中都不会追加省略号。
///指定字符串的截断字符串表示形式。
公共静态字符串截断(此字符串源,int-length,bool-appendellission=false)
{

如果(source.Length这对我有效-在我的Visual Studio中没有启动
truncate()
函数,因此我使用了
Substring()

Text=''

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

也许可以编写扩展方法?我也喜欢您的类型-但它的速度如何?对于简单的字符串长度剪切来说不是太多(也很慢?)@亚里士多德,。是的,哪个更快?当然linq在一个有20行的表上编译会慢一些,因为如果它不是静态的,那么编译20次就知道只需要剪切一个字符串。@亚里士多德,我希望你能读到这篇文章的目的是:你真的在乎吗?在这种情况下,我相信你不在乎,就像99.99%的时候一样。在每一次谈话中这听起来很有哲理性——但我关心速度,我关心快速程序,我喜欢使用快速程序。linq速度慢,实时编译太多-如果使用太多,最终会导致程序变慢。:)为了避免出现
if(lenght>140)
使用更多内存和时间。:)@亚里士多德,我知道,尤其是长度小于140时。但更糟糕的是:如果原始字符串(“我的文本”)长度小于140个字符,并且包含尾随空格,这些尾随空格会被截断。如果这些空格很重要,并且要在任何地方使用,并且每一点性能都很重要,我不建议使用此解决方案。然后,如前所述,这只是“另一种可能性”这是本页上最短的一个:-)我做了一个速度测试,第一个功能需要20毫秒,第二个功能需要600毫秒,Leniel需要5毫秒50000,你有580Ms的差异吗?没什么!!!如果你非常在意性能开始使用C甚至汇编。C不适合微优化。句型。
string.Concat('<%# Eval("My Text") %>'.ToString().Where((char, index) => index < 140))
< % # Eval("MyText").ToString().Length <= 30 ? Eval("MyText") : Eval("MyText").ToString().Substring(0, 30)+"..." % > 
    /// <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);
    }
Text='<%# Eval("LastChangedTime", "{0:t}").ToString().Substring(0,10) %>'