如何限制一个字符串,如果它比所需的大?
我目前有一个字符串,我想限制为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>
下面是一些代码,我用这样的事情。将其附加到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) + "…";
}
}
}
你的意思是像...
int maxLength = 200;
string trimmed = (trimmed.length > maxLength) ? trimmed.Substring(0,maxlength) : trimmed ;
我想这是在网格或一些事情......我会打电话的功能,并通过您的评估和演示作为参数:
我的例子:
<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
这也许豆蔻多日你需要,但在大多数情况下适合我。它保留文件结尾,如果你处理文件并在短尾字符串的末尾添加“...”(如果你想的话)。
/// <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
有一个Win32方法来做到这一点 - '[DllImport(“shlwapi.dll”,CharSet = CharSet.Auto)] static extern bool PathCompactPathEx([Out] StringBuilder pszOut,string szPath,int cchMax,int dwFlags); ' – 2010-07-22 10:29:41
不知道。感谢您的输入! – Remy 2010-07-24 10:33:36
这是一个合理的方法。但是根据OP的层次,我建议在数据模型上创建一个新字段,例如'ShortBigText',并在其中实现缩短操作。 – 2010-07-22 08:38:33
@silky - 一个很好的观点。我会让它成为一种方法,而不是属性,但是改变'BigText'属性会改变'ShortBigText'返回的值。这会违反财产独立。 – 2010-07-22 10:25:16