首页 > 其他 > 详细

截取字符串

时间:2016-08-25 23:53:03      阅读:460      评论:0      收藏:0      [点我收藏+]
#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
技术分享

截取字符串

原文:http://www.cnblogs.com/zhangxiaolei521/p/5808520.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!