如果字符串大于期望值,我怎么能限制它?

时间:2010-07-22 07:51:56

标签: asp.net string

我目前有一个字符串,我想限制为200个字符。

我不知道如何格式化,所以如果它更少,它就不会改变,但如果它更多,它将修剪它。

这是在ListView控件中,而不是中继器。对不起,我的错误。

<ItemTemplate>
<div class="portfolio_title">
<div class="custom_title">
<%# DataBinder.Eval(Container.DataItem, "Title")%></div>
</div>
<asp:Literal ID="LiteralArticle" runat="server"></asp:Literal>
<%# DataBinder.Eval(Container.DataItem, "Article")%><br />
<a href="NewsFull.aspx?id=<%# DataBinder.Eval(Container.DataItem, "id")%>">Read Full Article...</a>
<div class="page_line">
</div>
</ItemTemplate>

4 个答案:

答案 0 :(得分:3)

这是我用于此类事情的一些代码。将其附加到OnRowDataBound事件。这会截断为50个字符并添加elipses“...”。

protected void CommentGridViewRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TableCell cell = e.Row.Cells[0];

            if (!string.IsNullOrEmpty(cell.Text) && cell.Text.Length > 50)
            {
                cell.Text = cell.Text.Substring(0, 50) + "&hellip;";
            }
        }
    }

答案 1 :(得分:1)

你的意思是......

int maxLength = 200;
string trimmed = (trimmed.length > maxLength) ? trimmed.Substring(0,maxlength) : trimmed ;

答案 2 :(得分:1)

我认为这是在一个网格或其他东西......我会调用一个函数并将你的Eval作为一个参数传递:

我的例子:

<asp:Image ID="imgTopLevelTickCross" runat="server" ImageUrl='<%# "/images/" &  getImage(Eval("DrwgID").toString()) & ".gif" %> ' />

ImageURL调用getImage并将Eval(“DrwgID”)的值传递给它以形成src路径

Public Function getImage(ByVal drwgID As Integer) As String


If TopLevelDrwgID = drwgID Then

        Return "True"
    Else
        Return "blank"
    End If
End Function

答案 3 :(得分:1)

它可能比你需要的更多,但在大多数情况下对我来说效果很好。如果您处理文件,它会保留文件结尾,如果需要,可以在短尾字符串的末尾添加“...”。

    /// <summary>
    /// Shortens a long string. Optionally keeps the file ending and adds a placeholder at the end.
    /// </summary>
    /// <example>
    /// Input:  ThisIsAVeryLongFilenameForThisTest.doc (length=10, placeholder='...', saveFileEnding=true)
    /// Output: ThisIsAVeryLong
    /// </example>
    /// <param name="value"></param>
    /// <param name="length"></param>
    /// <param name="placeHolder"></param>
    /// <param name="saveFileEnding"></param>
    /// <returns></returns>
    public static string ShowSummary(string value, int length, string placeHolder, bool saveFileEnding)
    {
        int lengthNew = length;
        string fileEnding = "";

        //nothing to do if the string is short enough
        if (length > value.Length)
        {
            return value;
        }

        if (saveFileEnding)
        {
            int index = value.LastIndexOf(".");

            if (index != -1)
            {
                fileEnding = value.Substring(index);
                lengthNew = length - fileEnding.Length;
            }
        }

        //substract the length of the placeholder
        lengthNew = lengthNew - placeHolder.Length;

        if (lengthNew > 0)
        {
            return value.Substring(0, lengthNew) + placeHolder + fileEnding;
        }
        else
        {
            //something is weird, maybe a really long filending or a '.' in the filename, so just cut it down 
            return value.Substring(0, length);
        }
    }//ShowSummary