123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- using System;
- using System.Collections.Generic;
- 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
- /// <summary>
- /// 将字符串转换为int类型数组
- /// </summary>
- /// <param name="str">如1,2,3,4,5</param>
- /// <returns></returns>
- public static List<string> StrToListString(string str)
- {
- var list = new List<string>();
- if (!str.Contains(","))
- {
- list.Add(str);
- return list;
- }
- var slist = str.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
- foreach (var item in slist)
- {
- list.Add(item);
- }
- return list;
- }
- }
- }
|