StringHelper.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace TEAMModelOS.SDK.Helper.Common.StringHelper
  5. {
  6. public class StringHelper
  7. {
  8. #region 截取字符串
  9. public static string GetSubString(string pSrcString, int pLength, string pTailString)
  10. {
  11. return GetSubString(pSrcString, 0, pLength, pTailString);
  12. }
  13. public static string GetSubString(string pSrcString, int pStartIndex, int pLength, string pTailString)
  14. {
  15. string str = pSrcString;
  16. byte[] bytes = Encoding.UTF8.GetBytes(pSrcString);
  17. foreach (char ch in Encoding.UTF8.GetChars(bytes))
  18. {
  19. if (((ch > 'ࠀ') && (ch < '一')) || ((ch > 0xac00) && (ch < 0xd7a3)))
  20. {
  21. if (pStartIndex >= pSrcString.Length)
  22. {
  23. return "";
  24. }
  25. return pSrcString.Substring(pStartIndex, ((pLength + pStartIndex) > pSrcString.Length) ? (pSrcString.Length - pStartIndex) : pLength);
  26. }
  27. }
  28. if (pLength < 0)
  29. {
  30. return str;
  31. }
  32. byte[] sourceArray = Encoding.Default.GetBytes(pSrcString);
  33. if (sourceArray.Length <= pStartIndex)
  34. {
  35. return str;
  36. }
  37. int length = sourceArray.Length;
  38. if (sourceArray.Length > (pStartIndex + pLength))
  39. {
  40. length = pLength + pStartIndex;
  41. }
  42. else
  43. {
  44. pLength = sourceArray.Length - pStartIndex;
  45. pTailString = "";
  46. }
  47. int num2 = pLength;
  48. int[] numArray = new int[pLength];
  49. byte[] destinationArray = null;
  50. int num3 = 0;
  51. for (int i = pStartIndex; i < length; i++)
  52. {
  53. if (sourceArray[i] > 0x7f)
  54. {
  55. num3++;
  56. if (num3 == 3)
  57. {
  58. num3 = 1;
  59. }
  60. }
  61. else
  62. {
  63. num3 = 0;
  64. }
  65. numArray[i] = num3;
  66. }
  67. if ((sourceArray[length - 1] > 0x7f) && (numArray[pLength - 1] == 1))
  68. {
  69. num2 = pLength + 1;
  70. }
  71. destinationArray = new byte[num2];
  72. Array.Copy(sourceArray, pStartIndex, destinationArray, 0, num2);
  73. return (Encoding.Default.GetString(destinationArray) + pTailString);
  74. }
  75. #endregion
  76. /// <summary>
  77. /// 将字符串转换为int类型数组
  78. /// </summary>
  79. /// <param name="str">如1,2,3,4,5</param>
  80. /// <returns></returns>
  81. public static List<string> StrToListString(string str)
  82. {
  83. var list = new List<string>();
  84. if (!str.Contains(","))
  85. {
  86. list.Add(str);
  87. return list;
  88. }
  89. var slist = str.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  90. foreach (var item in slist)
  91. {
  92. list.Add(item);
  93. }
  94. return list;
  95. }
  96. }
  97. }