We often do one thing, is in the article system, the interception of a certain length of the article title, more than the specified length, add "..."
such as two strings:
String str1 = "Chinese people want AH abc ~";
String str2 = "1 Chinese 23456abc ~";
To intercept, after the output:
str1 = "Chinese people want ...";
str2 = "1 Chinese 2 ...";
That is, the string to be mixed in Chinese and English, after the interception, the length should be consistent, that is, 8 bytes length (excluding three points), and can not appear in the middle of the case of truncated. So I wrote a method:
public static string getStr(string s,int l)
{
string temp = s ;
if (Regex.Replace(temp,"[\u4e00-\u9fa5]","zz",RegexOptions.IgnoreCase).Length<=l)
{
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<=l-3)
{
return temp + "";
}
}
return "";
}
Call:
String content = "Chinese AH abc yes Ah yes";
Content = Getstr (content,13);
Http://yao.cnblogs.com/archive/2006/07/04/442886.html