字符串处理类

 1 using System;
 2 using System.Configuration;
 3 using System.Collections;
 4 using System.Collections.Generic;
 5 using System.Text;
 6 
 7 namespace SQLServerDAL
 8 {
 9     /// <summary>
10     /// 字符串处理类
11     /// </summary>
12     public class FomatString
13     {
14         #region 字符串截取函数
15         /// <summary>
16         /// 截取字符串方法
17         /// </summary>
18         /// <param name="inputString">要截取的原字符串</param>
19         /// <param name="len">要截取的长度</param>
20         /// <returns>截取后的字符串</returns>
21         public string CutString(string inputString, int len)
22         {
23             ASCIIEncoding ascii = new ASCIIEncoding();
24             int tempLen = 0;
25             string tempString = "";
26             byte[] s = ascii.GetBytes(inputString);
27             for (int i = 0; i < s.Length; i++)
28             {
29                 if ((int)s[i] == 63)
30                 {
31                     tempLen += 2;
32                 }
33                 else
34                 {
35                     tempLen += 1;
36                 }
37 
38                 try
39                 {
40                     tempString += inputString.Substring(i, 1);
41                 }
42                 catch
43                 {
44                     break;
45                 }
46 
47                 if (tempLen > len)
48                     break;
49             }
50             //如果截过则加上半个省略号
51             byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString);
52             if (mybyte.Length > len)
53                 tempString += "";
54 
55 
56             return tempString;
57         }
58         #endregion
59     }
60 }

posted on 2008-12-28 14:28  鱼跃于渊  阅读(192)  评论(0编辑  收藏  举报

导航