Thursday, September 23, 2010

In asp.net how to specify the length of the interception of mixed Chinese and English strings?


One of the things we often do, that is, the article system, the interception of a certain length of the title of the article, more than the specified length to add "..."
If two strings: string str1 = "abc Chinese English ~"; string str2 = "136 136 ~ web site construction web136";
To intercept, the output:

str1 = "Chinese ..."; str2 = "136 136 ..."; Website
Which is to turn the string in English mixed in the interception, the length to be consistent, that is 8 bytes in length (excluding the three points), and the Chinese can not appear to be cut off from the middle of the situation. Then write 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 web language"; content = getStr (content, 13);

No comments:

Post a Comment