0730-8232398,8242200
上面这个字符串,如果考虑“,”是全角的情况,该如何做?下面我用的这个是错误的,没有正确判断符号是全角的情况,全角的符号用正则表达式该如何取呢?
public static string GetStrLen(string s, int len, string style)
{
string temp = s;
if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= len)
{
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 <= len - style.Length)
{
return temp + style;
}
}
return "";
}
上面这个字符串,如果考虑“,”是全角的情况,该如何做?下面我用的这个是错误的,没有正确判断符号是全角的情况,全角的符号用正则表达式该如何取呢?
public static string GetStrLen(string s, int len, string style)
{
string temp = s;
if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= len)
{
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 <= len - style.Length)
{
return temp + style;
}
}
return "";
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货