我有这个代码,是否有一种简单的方法来限制显示的字符数量为250?
<%# trimIt(DataBinder.Eval(Container.DataItem, "WebSalesText").ToString())%>
public string trimIt(string s)
{
if (s.Length > 0 && s.IndexOf(".") > 0)
{
return (s.Substring(0, s.IndexOf(".")) + " ...");
}
else
{
return s;
}
}
Run Code Online (Sandbox Code Playgroud)
Lor*_*nVS 21
你在寻找trimIt的实现吗?
public static string trimIt(string s)
{
if(s == null)
return string.Empty;
int count = Math.Min(s.Length, 250);
return s.Substring(0, count);
}
Run Code Online (Sandbox Code Playgroud)
您可以为字符串创建一个扩展方法来执行您需要的操作,并允许您指定允许的最大长度.
public static string TrimToMaxSize(this string input, int max)
{
return ((input != null) && (input.Length > max)) ?
input.Substring(0, max) : input;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7486 次 |
| 最近记录: |