zoukankan      html  css  js  c++  java
  • 截取字符串

    #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
    复制代码
  • 相关阅读:
    Java:day4
    Java:day3
    Java:day2
    Java学习的第一天
    void指针
    const* 和 *const
    指针数组和数组指针
    c++的const
    C语言基础总结
    C、C++、java中的static区别
  • 原文地址:https://www.cnblogs.com/zhangxiaolei521/p/5808520.html
Copyright © 2011-2022 走看看