StringHelper.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Text;
  3. namespace TEAMModelOS.SDK.Helper.Common.StringHelper
  4. {
  5. public class StringHelper
  6. {
  7. #region 截取字符串
  8. public static string GetSubString(string pSrcString, int pLength, string pTailString)
  9. {
  10. return GetSubString(pSrcString, 0, pLength, pTailString);
  11. }
  12. public static string GetSubString(string pSrcString, int pStartIndex, int pLength, string pTailString)
  13. {
  14. string str = pSrcString;
  15. byte[] bytes = Encoding.UTF8.GetBytes(pSrcString);
  16. foreach (char ch in Encoding.UTF8.GetChars(bytes))
  17. {
  18. if (((ch > 'ࠀ') && (ch < '一')) || ((ch > 0xac00) && (ch < 0xd7a3)))
  19. {
  20. if (pStartIndex >= pSrcString.Length)
  21. {
  22. return "";
  23. }
  24. return pSrcString.Substring(pStartIndex, ((pLength + pStartIndex) > pSrcString.Length) ? (pSrcString.Length - pStartIndex) : pLength);
  25. }
  26. }
  27. if (pLength < 0)
  28. {
  29. return str;
  30. }
  31. byte[] sourceArray = Encoding.Default.GetBytes(pSrcString);
  32. if (sourceArray.Length <= pStartIndex)
  33. {
  34. return str;
  35. }
  36. int length = sourceArray.Length;
  37. if (sourceArray.Length > (pStartIndex + pLength))
  38. {
  39. length = pLength + pStartIndex;
  40. }
  41. else
  42. {
  43. pLength = sourceArray.Length - pStartIndex;
  44. pTailString = "";
  45. }
  46. int num2 = pLength;
  47. int[] numArray = new int[pLength];
  48. byte[] destinationArray = null;
  49. int num3 = 0;
  50. for (int i = pStartIndex; i < length; i++)
  51. {
  52. if (sourceArray[i] > 0x7f)
  53. {
  54. num3++;
  55. if (num3 == 3)
  56. {
  57. num3 = 1;
  58. }
  59. }
  60. else
  61. {
  62. num3 = 0;
  63. }
  64. numArray[i] = num3;
  65. }
  66. if ((sourceArray[length - 1] > 0x7f) && (numArray[pLength - 1] == 1))
  67. {
  68. num2 = pLength + 1;
  69. }
  70. destinationArray = new byte[num2];
  71. Array.Copy(sourceArray, pStartIndex, destinationArray, 0, num2);
  72. return (Encoding.Default.GetString(destinationArray) + pTailString);
  73. }
  74. #endregion
  75. }
  76. }