当前位置: 移动技术网 > IT编程>开发语言>c# > c#完美截断字符串代码(中文+非中文)

c#完美截断字符串代码(中文+非中文)

2019年07月18日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:

public static string truncation(this htmlhelper htmlhelper, string str, int len)
{
if (str == null || str.length == 0 || len <= 0)
{
return string.empty;
}
int l = str.length;
#region 计算长度
int clen = 0;
while (clen < len && clen < l)
{
//每遇到一个中文,则将目标长度减一。
if ((int)str[clen] > 128) { len--; }
clen++;
}
#endregion
if (clen < l)
{
return str.substring(0, clen) + "...";
}
else
{
return str;
}
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网