/// 截断显示内容
/// </summary>
public string GetCut(object obj)
{
string temp = Server.HtmlDecode(obj as string);
//参数说明:要处理的字符串,符合条件的表达式[汉字],
//替换的字符[内容随意写但是要两个字符,因为一个中文对应两个字符,不区分大小写]
if (Regex.Replace(temp, "[/u4e00-/u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= 30)
{
return temp;
}
for (int i = temp.Length; i >= 0; i--)
{
temp = temp.Substring(0, i);
if (Regex.Replace(temp, "[/u4e00-/u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= 30 - 3)
{
return temp + "...";
}
}
return "...";
}
如果前台的GridView控件中调用的话:直接<%# GetCut(Eval("content"))%>
截取文章内容长度
最新推荐文章于 2022-03-15 10:34:02 发布