StringHelper.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.Json;
  5. namespace HTEXLib.COMM.Helpers
  6. {
  7. public class StringHelper
  8. {
  9. #region 截取字符串
  10. public static string GetSubString(string pSrcString, int pLength, string pTailString)
  11. {
  12. return GetSubString(pSrcString, 0, pLength, pTailString);
  13. }
  14. public static string GetSubString(string pSrcString, int pStartIndex, int pLength, string pTailString)
  15. {
  16. string str = pSrcString;
  17. byte[] bytes = Encoding.UTF8.GetBytes(pSrcString);
  18. foreach (char ch in Encoding.UTF8.GetChars(bytes))
  19. {
  20. if (((ch > 'ࠀ') && (ch < '一')) || ((ch > 0xac00) && (ch < 0xd7a3)))
  21. {
  22. if (pStartIndex >= pSrcString.Length)
  23. {
  24. return "";
  25. }
  26. return pSrcString.Substring(pStartIndex, ((pLength + pStartIndex) > pSrcString.Length) ? (pSrcString.Length - pStartIndex) : pLength);
  27. }
  28. }
  29. if (pLength < 0)
  30. {
  31. return str;
  32. }
  33. byte[] sourceArray = Encoding.Default.GetBytes(pSrcString);
  34. if (sourceArray.Length <= pStartIndex)
  35. {
  36. return str;
  37. }
  38. int length = sourceArray.Length;
  39. if (sourceArray.Length > (pStartIndex + pLength))
  40. {
  41. length = pLength + pStartIndex;
  42. }
  43. else
  44. {
  45. pLength = sourceArray.Length - pStartIndex;
  46. pTailString = "";
  47. }
  48. int num2 = pLength;
  49. int[] numArray = new int[pLength];
  50. byte[] destinationArray = null;
  51. int num3 = 0;
  52. for (int i = pStartIndex; i < length; i++)
  53. {
  54. if (sourceArray[i] > 0x7f)
  55. {
  56. num3++;
  57. if (num3 == 3)
  58. {
  59. num3 = 1;
  60. }
  61. }
  62. else
  63. {
  64. num3 = 0;
  65. }
  66. numArray[i] = num3;
  67. }
  68. if ((sourceArray[length - 1] > 0x7f) && (numArray[pLength - 1] == 1))
  69. {
  70. num2 = pLength + 1;
  71. }
  72. destinationArray = new byte[num2];
  73. Array.Copy(sourceArray, pStartIndex, destinationArray, 0, num2);
  74. return (Encoding.Default.GetString(destinationArray) + pTailString);
  75. }
  76. #endregion
  77. /// <summary>
  78. /// 将字符串转换为int类型数组
  79. /// </summary>
  80. /// <param name="str">如1,2,3,4,5</param>
  81. /// <returns></returns>
  82. public static List<string> StrToListString(string str)
  83. {
  84. var list = new List<string>();
  85. if (!str.Contains(","))
  86. {
  87. list.Add(str);
  88. return list;
  89. }
  90. var slist = str.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  91. foreach (var item in slist)
  92. {
  93. list.Add(item);
  94. }
  95. return list;
  96. }
  97. /// <summary>
  98. /// 截取两个字符串中间的字符串
  99. /// </summary>
  100. /// <param name="sourse"></param>
  101. /// <param name="startstr"></param>
  102. /// <param name="endstr"></param>
  103. /// <returns></returns>
  104. public static string SubMidString(string sourse, string startstr, string endstr)
  105. {
  106. string result = string.Empty;
  107. int startindex, endindex;
  108. startindex = sourse.IndexOf(startstr);
  109. if (startindex == -1)
  110. return result;
  111. string tmpstr = sourse.Substring(startindex + startstr.Length);
  112. endindex = tmpstr.IndexOf(endstr);
  113. if (endindex == -1)
  114. return result;
  115. result = tmpstr.Remove(endindex);
  116. return result;
  117. }
  118. public static double[] ListTodouble(List<double> list)
  119. {
  120. Double[] doubles = new Double[list.Count];
  121. int k = 0;
  122. foreach (double p in list)
  123. {
  124. doubles[k] = p;
  125. k++;
  126. }
  127. //doubles = list;
  128. if (doubles == null)
  129. {
  130. return null;
  131. }
  132. double[] result = new double[doubles.Length];
  133. for (int i = 0; i < doubles.Length; i++)
  134. {
  135. result[i] = doubles[i];
  136. }
  137. return result;
  138. }
  139. public static double[,] ListToDouble(List<List<double>> list)
  140. {
  141. int itemCount = 0;
  142. //int stuCount = list.Count;
  143. foreach (List<double> p in list)
  144. {
  145. if (p.Count != 0)
  146. {
  147. itemCount = p.Count;
  148. break;
  149. }
  150. }
  151. for (int k = 0; k < list.Count; k++)
  152. {
  153. if (list[k].Count == 0)
  154. {
  155. list.Remove(list[k]);
  156. }
  157. }
  158. double[,] doubles = new double[list.Count, itemCount];
  159. for (int i = 0; i < list.Count; i++)
  160. {
  161. for (int j = 0; j < itemCount; j++)
  162. {
  163. doubles[i, j] = list[i][j];
  164. }
  165. }
  166. return doubles;
  167. }
  168. public static int getKeyCount(JsonElement element)
  169. {
  170. int keys = 0;
  171. var emobj = element.EnumerateObject();
  172. while (emobj.MoveNext())
  173. {
  174. keys++;
  175. }
  176. return keys;
  177. }
  178. public static string getEduName(string key)
  179. {
  180. string name = "";
  181. switch (key)
  182. {
  183. case "0":
  184. name = "知识";
  185. break;
  186. case "1":
  187. name = "理解";
  188. break;
  189. case "2":
  190. name = "应用";
  191. break;
  192. case "3":
  193. name = "分析";
  194. break;
  195. case "4":
  196. name = "综合";
  197. break;
  198. case "5":
  199. name = "评鉴";
  200. break;
  201. }
  202. return name;
  203. }
  204. public static string getTypeName(string key)
  205. {
  206. string name = "";
  207. switch (key)
  208. {
  209. case "0":
  210. name = "Single";
  211. break;
  212. case "1":
  213. name = "Multiple";
  214. break;
  215. case "2":
  216. name = "Judge";
  217. break;
  218. case "3":
  219. name = "Complete";
  220. break;
  221. }
  222. return name;
  223. }
  224. }
  225. }