截取字符串

#region 截取字符串
 2         public static string GetSubString(string p_SrcString, int p_Length, string p_TailString)
 3         {
 4             return GetSubString(p_SrcString, 0, p_Length, p_TailString);
 5         }
 6         public static string GetSubString(string p_SrcString, int p_StartIndex, int p_Length, string p_TailString)
 7         {
 8             string str = p_SrcString;
 9             byte[] bytes = Encoding.UTF8.GetBytes(p_SrcString);
10             foreach (char ch in Encoding.UTF8.GetChars(bytes))
11             {
12                 if (((ch > 'ࠀ') && (ch < '一')) || ((ch > 0xac00) && (ch < 0xd7a3)))
13                 {
14                     if (p_StartIndex >= p_SrcString.Length)
15                     {
16                         return "";
17                     }
18                     return p_SrcString.Substring(p_StartIndex, ((p_Length + p_StartIndex) > p_SrcString.Length) ? (p_SrcString.Length - p_StartIndex) : p_Length);
19                 }
20             }
21             if (p_Length < 0)
22             {
23                 return str;
24             }
25             byte[] sourceArray = Encoding.Default.GetBytes(p_SrcString);
26             if (sourceArray.Length <= p_StartIndex)
27             {
28                 return str;
29             }
30             int length = sourceArray.Length;
31             if (sourceArray.Length > (p_StartIndex + p_Length))
32             {
33                 length = p_Length + p_StartIndex;
34             }
35             else
36             {
37                 p_Length = sourceArray.Length - p_StartIndex;
38                 p_TailString = "";
39             }
40             int num2 = p_Length;
41             int[] numArray = new int[p_Length];
42             byte[] destinationArray = null;
43             int num3 = 0;
44             for (int i = p_StartIndex; i < length; i++)
45             {
46                 if (sourceArray[i] > 0x7f)
47                 {
48                     num3++;
49                     if (num3 == 3)
50                     {
51                         num3 = 1;
52                     }
53                 }
54                 else
55                 {
56                     num3 = 0;
57                 }
58                 numArray[i] = num3;
59             }
60             if ((sourceArray[length - 1] > 0x7f) && (numArray[p_Length - 1] == 1))
61             {
62                 num2 = p_Length + 1;
63             }
64             destinationArray = new byte[num2];
65             Array.Copy(sourceArray, p_StartIndex, destinationArray, 0, num2);
66             return (Encoding.Default.GetString(destinationArray) + p_TailString);
67         }
68         #endregion
复制代码
posted on 2016-08-25 22:00  張暁磊  阅读(159)  评论(0编辑  收藏  举报