1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- using System;
- using System.Text;
- namespace TEAMModelOS.SDK.Helper.Common.StringHelper
- {
- public class StringHelper
- {
- #region 截取字符串
- public static string GetSubString(string pSrcString, int pLength, string pTailString)
- {
- return GetSubString(pSrcString, 0, pLength, pTailString);
- }
- public static string GetSubString(string pSrcString, int pStartIndex, int pLength, string pTailString)
- {
- string str = pSrcString;
- byte[] bytes = Encoding.UTF8.GetBytes(pSrcString);
- foreach (char ch in Encoding.UTF8.GetChars(bytes))
- {
- if (((ch > 'ࠀ') && (ch < '一')) || ((ch > 0xac00) && (ch < 0xd7a3)))
- {
- if (pStartIndex >= pSrcString.Length)
- {
- return "";
- }
- return pSrcString.Substring(pStartIndex, ((pLength + pStartIndex) > pSrcString.Length) ? (pSrcString.Length - pStartIndex) : pLength);
- }
- }
- if (pLength < 0)
- {
- return str;
- }
- byte[] sourceArray = Encoding.Default.GetBytes(pSrcString);
- if (sourceArray.Length <= pStartIndex)
- {
- return str;
- }
- int length = sourceArray.Length;
- if (sourceArray.Length > (pStartIndex + pLength))
- {
- length = pLength + pStartIndex;
- }
- else
- {
- pLength = sourceArray.Length - pStartIndex;
- pTailString = "";
- }
- int num2 = pLength;
- int[] numArray = new int[pLength];
- byte[] destinationArray = null;
- int num3 = 0;
- for (int i = pStartIndex; i < length; i++)
- {
- if (sourceArray[i] > 0x7f)
- {
- num3++;
- if (num3 == 3)
- {
- num3 = 1;
- }
- }
- else
- {
- num3 = 0;
- }
- numArray[i] = num3;
- }
- if ((sourceArray[length - 1] > 0x7f) && (numArray[pLength - 1] == 1))
- {
- num2 = pLength + 1;
- }
- destinationArray = new byte[num2];
- Array.Copy(sourceArray, pStartIndex, destinationArray, 0, num2);
- return (Encoding.Default.GetString(destinationArray) + pTailString);
- }
- #endregion
- }
- }
|