namespace JLHHJSvr.Helper
{
public static class StringEx
{
///
/// 按中文代表两字符的方式发截断字符串(返回字符串的长度小于等于len)
///
/// 原字符串
/// 长度
///
public static string SubStringEx(this string str, int len)
{
if (string.IsNullOrEmpty(str) || 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 (len < 0)
{
len = 0;
}
if (len > str.Length)
{
len = str.Length;
}
return str.Substring(0, len);
}
}
}