c#完美截断字符串代码(中文+非中文)
2018-10-12 14:09
447 查看
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;
}
}
您可能感兴趣的文章:
相关文章推荐
- c#完美截断字符串代码(中文+非中文)
- c#完美截断字符串(中文+非中文)
- 写了一个字符串截取一定长度的代码,中文两个字符,英文一个字符,如果截断了自动用一个.或两个.来对齐,用于标题的显示
- 经验总结:应对中文输入法的字符串截断方案(带代码示例)
- 转载--经验总结:应对中文输入法的字符串截断方案(带代码示例)
- c#实现16进制和字符串之间转换的代码
- C#动态执行字符串(动态创建代码)
- C#、Java实现按字节截取字符串包含中文汉字和英文字符数字标点符号等
- C# 判断中文字符(字符串)-整理
- java中文字符串按拼音排序 代码(转)
- c# 反射执行字符串表达示代码
- C#中根据字节处理字符串(含中文字符)
- c#实现16进制和字符串之间转换的代码
- 在C#中截取指定长度的中文字符串(效率提高2500倍)
- C# 中文简体转繁体实现代码
- C#判断中文字符(字符串)
- php中判断字符串是否全是中文或含有中文的实现代码
- C#连接远程oracle字符串,返回操作时间代码
- Jquery截取中文字符串的实现代码
- C#去除字符串中的中文、字母、数字