StringHelper.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. /// <summary>
  97. /// 截取两个字符串中间的字符串
  98. /// </summary>
  99. /// <param name="sourse"></param>
  100. /// <param name="startstr"></param>
  101. /// <param name="endstr"></param>
  102. /// <returns></returns>
  103. public static string SubMidString(string sourse, string startstr, string endstr)
  104. {
  105. string result = string.Empty;
  106. int startindex, endindex;
  107. startindex = sourse.IndexOf(startstr);
  108. if (startindex == -1)
  109. return result;
  110. string tmpstr = sourse.Substring(startindex + startstr.Length);
  111. endindex = tmpstr.IndexOf(endstr);
  112. if (endindex == -1)
  113. return result;
  114. result = tmpstr.Remove(endindex);
  115. return result;
  116. }
  117. public static double[] ListTodouble(List<double> list)
  118. {
  119. Double[] doubles = new Double[list.Count];
  120. int k = 0;
  121. foreach (double p in list)
  122. {
  123. doubles[k] = p;
  124. k++;
  125. }
  126. //doubles = list;
  127. if (doubles == null)
  128. {
  129. return null;
  130. }
  131. double[] result = new double[doubles.Length];
  132. for (int i = 0; i < doubles.Length; i++)
  133. {
  134. result[i] = doubles[i];
  135. }
  136. return result;
  137. }
  138. public static double[,] ListToDouble(List<List<double>> list)
  139. {
  140. int itemCount = 0;
  141. //int stuCount = list.Count;
  142. foreach (List<double> p in list)
  143. {
  144. if (p.Count != 0)
  145. {
  146. itemCount = p.Count;
  147. break;
  148. }
  149. }
  150. for (int k = 0; k < list.Count; k++)
  151. {
  152. if (list[k].Count == 0)
  153. {
  154. list.Remove(list[k]);
  155. }
  156. }
  157. double[,] doubles = new double[list.Count, itemCount];
  158. for (int i = 0; i < list.Count; i++)
  159. {
  160. for (int j = 0; j < itemCount; j++)
  161. {
  162. doubles[i, j] = list[i][j];
  163. }
  164. }
  165. return doubles;
  166. }
  167. }
  168. }