1 //中英混合字符串截断
2 public static string getStr(string s, int l)
3 {
4 string temp = s;
5 if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= l)
6 {
7 return temp;
8 }
9 for (int i = temp.Length; i >= 0; i--)
10 {
11 temp = temp.Substring(0, i);
12 if (Regex.Replace(temp, "[\u4e00-\u9fa5]", "zz", RegexOptions.IgnoreCase).Length <= l - 3)
13 {
14 return temp + "...";
15 }
16 }
17 return "...";
18 }
19
20
21
22 C#中文字符截取函数
23 ///str_value 字符
24 ///str_len 要截取的字符长度
25 public string leftx(string str_value,int str_len)
26 {
27 int p_num = 0;
28 int i;
29 string New_Str_value = "";
30
31 if (str_value=="")
32 {
33 New_Str_value = "";
34 }
35 else
36 {
37 int Len_Num = str_value.Length;
38
39
40
41 //if (Len_Num < str_len)
42 //{
43 // str_len = Len_Num;
44 //}
45
46
47 for (i = 0;i<=Len_Num - 1; i++)
48 {
49 //str_value.Substring(i,1);
50 if (i >Len_Num) break;
51 char c = Convert.ToChar(str_value.Substring(i,1));
52 if (((int)c > 255) || ((int)c<0))
53 {
54 p_num = p_num + 2;
55
56 }
57 else
58 {
59 p_num = p_num + 1;
60
61 }
62
63 if (p_num >= str_len)
64 {
65
66 New_Str_value = str_value.Substring(0,i+1);
67
68 break;
69 }
70 else
71 {
72 New_Str_value = str_value;
73 }
74
75 }
76
77 }
78 return New_Str_value;
79 }
80
81
82
83 //数据的自动换行
84
85 <table>
86 <tr>
87 <td style="word-wrap:break-word;width=100">
88 绑定你的数据,会照原样显示,长度超过100就自动折行。
89 </td>
90 </tr>
91 </table>