Code
1 字符串截取函数#region 字符串截取函数
2 public static string CutString(string inputString, int len)
3 {
4
5 ASCIIEncoding ascii = new ASCIIEncoding();
6 int tempLen = 0;
7 string tempString = "";
8 byte[] s = ascii.GetBytes(inputString);
9 for (int i = 0; i < s.Length; i++)
10 {
11 if ((int)s[i] == 63)
12 {
13 tempLen += 2;
14 }
15 else
16 {
17 tempLen += 1;
18 }
19
20 try
21 {
22 tempString += inputString.Substring(i, 1);
23 }
24 catch
25 {
26 break;
27 }
28
29 if (tempLen > len)
30 break;
31 }
32 byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString);
33 if (mybyte.Length > len)
34 tempString += "…";
35
36 return tempString;
37 }
38 #endregion
Repeat中调用该函数:
1 字符串截取函数#region 字符串截取函数
2 public static string CutString(string inputString, int len)
3 {
4
5 ASCIIEncoding ascii = new ASCIIEncoding();
6 int tempLen = 0;
7 string tempString = "";
8 byte[] s = ascii.GetBytes(inputString);
9 for (int i = 0; i < s.Length; i++)
10 {
11 if ((int)s[i] == 63)
12 {
13 tempLen += 2;
14 }
15 else
16 {
17 tempLen += 1;
18 }
19
20 try
21 {
22 tempString += inputString.Substring(i, 1);
23 }
24 catch
25 {
26 break;
27 }
28
29 if (tempLen > len)
30 break;
31 }
32 byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString);
33 if (mybyte.Length > len)
34 tempString += "…";
35
36 return tempString;
37 }
38 #endregion
Code
1 <asp:Repeater ID="Repeater1" runat="server">
2 <ItemTemplate>
3 <a href='<%# "Bulletin/Details.aspx?BulletinID="+ DataBinder.Eval(Container.DataItem,"ID") %>'><%# Warning.CutString(DataBinder.Eval(Container.DataItem, "Title").ToString(),22)%></a>
4 </ItemTemplate>
5 </asp:Repeater>
1 <asp:Repeater ID="Repeater1" runat="server">
2 <ItemTemplate>
3 <a href='<%# "Bulletin/Details.aspx?BulletinID="+ DataBinder.Eval(Container.DataItem,"ID") %>'><%# Warning.CutString(DataBinder.Eval(Container.DataItem, "Title").ToString(),22)%></a>
4 </ItemTemplate>
5 </asp:Repeater>
去除最后那个“|”字符
Code
1 private string ClearLastChar(string str)
2 {
3 if (str == null)
4 {
5 return str;
6 }
7 else
8 {
9 if (str.Length > 0)
10 {
11 return str.Substring(0, str.LastIndexOf("|"));
12 }
13 else
14 {
15 return "";
16 }
17
18 }
19 }
20
1 private string ClearLastChar(string str)
2 {
3 if (str == null)
4 {
5 return str;
6 }
7 else
8 {
9 if (str.Length > 0)
10 {
11 return str.Substring(0, str.LastIndexOf("|"));
12 }
13 else
14 {
15 return "";
16 }
17
18 }
19 }
20