StringUtility.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Text.RegularExpressions;
  5. using System;
  6. using System.Text;
  7. using System.Linq;
  8. using System.Runtime.CompilerServices;
  9. public static class StringUtility
  10. {
  11. #region --- AddColor ---
  12. public static string AddColor(object obj,Color color)
  13. {
  14. return AddColor(obj,color);
  15. }
  16. public static string AddColor(this string str,Color color)
  17. {
  18. //把颜色转换为16进制字符串,添加到富文本
  19. return string.Format("<color=#{0}>{1}</color>",ColorUtility.ToHtmlStringRGBA(color),str);
  20. }
  21. public static string AddColor(string str1,string str2,Color color)
  22. {
  23. return AddColor(str1 + str2,color);
  24. }
  25. public static string AddColor(string str1,string str2,string str3,Color color)
  26. {
  27. return AddColor(str1 + str2 + str3,color);
  28. }
  29. #endregion
  30. #region --- string length ---
  31. /// <summary>
  32. /// 化简字符串长度
  33. /// </summary>
  34. /// <param name="targetStr"></param>
  35. /// <param name="targetLength">目标长度,英文字符==1,中文字符==2</param>
  36. /// <returns></returns>
  37. public static string AbbrevStringWithinLength(string targetStr,int targetLength,string abbrevPostfix)
  38. {
  39. //C#实际统计:一个中文字符长度==1,英文字符长度==1
  40. //UI显示要求:一个中文字符长度==2,英文字符长度==1
  41. //校验参数
  42. if(string.IsNullOrEmpty(targetStr) || targetLength <= 0)
  43. return targetStr;
  44. //字符串长度 * 2 <= 目标长度,即使是全中文也在长度范围内
  45. if(targetStr.Length * 2 <= targetLength)
  46. return targetStr;
  47. //遍历字符
  48. char[] chars = targetStr.ToCharArray();
  49. int curLen = 0;
  50. for(int i = 0; i < chars.Length; i++)
  51. {
  52. //累加字符串长度
  53. if(chars[i] >= 0x4e00 && chars[i] <= 0x9fbb)
  54. curLen += 2;
  55. else
  56. curLen += 1;
  57. //如果当前位置累计长度超过目标长度,取0~i-1,即Substring(0,i)
  58. if(curLen > targetLength)
  59. return targetStr.Substring(0,i) + abbrevPostfix;
  60. }
  61. return targetStr;
  62. }
  63. #endregion
  64. #region --- String To Array ---
  65. //string
  66. public static byte StringToByte(string valueStr)
  67. {
  68. byte value;
  69. if(byte.TryParse(valueStr,out value))
  70. return value;
  71. else
  72. return 0;
  73. }
  74. public static string[] StringToStringArray(string valueStr,char splitSign = '|')
  75. {
  76. if(string.IsNullOrEmpty(valueStr))
  77. return null;
  78. return valueStr.Split(splitSign);
  79. }
  80. public static StringArr[] StringToStringArray2D(string valueStr,char splitSign1 = '&',char splitSign2 = '|')
  81. {
  82. if(string.IsNullOrEmpty(valueStr))
  83. return null;
  84. string[] strArr1 = valueStr.Split(splitSign1);
  85. if(strArr1.Length == 0)
  86. return null;
  87. StringArr[] arrArr = new StringArr[strArr1.Length];
  88. for(int i = 0; i < strArr1.Length; i++)
  89. {
  90. arrArr[i] = new StringArr()
  91. {
  92. array = strArr1[i].Split(splitSign2)
  93. };
  94. }
  95. return arrArr;
  96. }
  97. //int
  98. public static int StringToInt(string valueStr)
  99. {
  100. int value;
  101. if(int.TryParse(valueStr,out value))
  102. return value;
  103. else
  104. return 0;
  105. }
  106. public static int[] StringToIntArray(string valueStr,char splitSign = '|')
  107. {
  108. if(string.IsNullOrEmpty(valueStr))
  109. return null;
  110. string[] valueArr = valueStr.Split(splitSign);
  111. if(valueArr == null || valueArr.Length == 0)
  112. return null;
  113. int[] intArr = new int[valueArr.Length];
  114. for(int i = 0; i < valueArr.Length; i++)
  115. {
  116. intArr[i] = StringToInt(valueArr[i]);
  117. }
  118. return intArr;
  119. }
  120. public static IntArr[] StringToIntArray2D(string valueStr,char splitSign1 = '&',char splitSign2 = '|')
  121. {
  122. if(string.IsNullOrEmpty(valueStr))
  123. return null;
  124. string[] strArr1 = valueStr.Split(splitSign1);
  125. if(strArr1.Length == 0)
  126. return null;
  127. IntArr[] arrArr = new IntArr[strArr1.Length];
  128. for(int i = 0; i < strArr1.Length; i++)
  129. {
  130. arrArr[i] = new IntArr()
  131. {
  132. array = StringToIntArray(strArr1[i],splitSign2)
  133. };
  134. }
  135. return arrArr;
  136. }
  137. //float
  138. public static float StringToFloat(string valueStr)
  139. {
  140. float value;
  141. if(float.TryParse(valueStr,out value))
  142. return value;
  143. else
  144. return 0;
  145. }
  146. public static float[] StringToFloatArray(string valueStr,char splitSign = '|')
  147. {
  148. if(string.IsNullOrEmpty(valueStr))
  149. return null;
  150. string[] valueArr = valueStr.Split(splitSign);
  151. if(valueArr == null || valueArr.Length == 0)
  152. return null;
  153. float[] floatArr = new float[valueArr.Length];
  154. for(int i = 0; i < valueArr.Length; i++)
  155. {
  156. floatArr[i] = StringToFloat(valueArr[i]);
  157. }
  158. return floatArr;
  159. }
  160. public static FloatArr[] StringToFloatArray2D(string valueStr,char splitSign1 = '&',char splitSign2 = '|')
  161. {
  162. if(string.IsNullOrEmpty(valueStr))
  163. return null;
  164. string[] strArr1 = valueStr.Split(splitSign1);
  165. if(strArr1.Length == 0)
  166. return null;
  167. FloatArr[] arrArr = new FloatArr[strArr1.Length];
  168. for(int i = 0; i < strArr1.Length; i++)
  169. {
  170. arrArr[i] = new FloatArr()
  171. {
  172. array = StringToFloatArray(strArr1[i],splitSign2)
  173. };
  174. }
  175. return arrArr;
  176. }
  177. //bool
  178. public static bool StringToBool(string valueStr)
  179. {
  180. bool value;
  181. if(bool.TryParse(valueStr,out value))
  182. return value;
  183. else
  184. return false;
  185. }
  186. public static bool[] StringToBoolArray(string valueStr,char splitSign = '|')
  187. {
  188. if(string.IsNullOrEmpty(valueStr))
  189. return null;
  190. string[] valueArr = valueStr.Split(splitSign);
  191. if(valueArr == null || valueArr.Length == 0)
  192. return null;
  193. bool[] boolArr = new bool[valueArr.Length];
  194. for(int i = 0; i < valueArr.Length; i++)
  195. {
  196. boolArr[i] = StringToBool(valueArr[i]);
  197. }
  198. return boolArr;
  199. }
  200. public static BoolArr[] StringToBoolArray2D(string valueStr,char splitSign1 = '&',char splitSign2 = '|')
  201. {
  202. if(string.IsNullOrEmpty(valueStr))
  203. return null;
  204. string[] strArr1 = valueStr.Split(splitSign1);
  205. if(strArr1.Length == 0)
  206. return null;
  207. BoolArr[] arrArr = new BoolArr[strArr1.Length];
  208. for(int i = 0; i < strArr1.Length; i++)
  209. {
  210. arrArr[i] = new BoolArr()
  211. {
  212. array = StringToBoolArray(strArr1[i],splitSign2)
  213. };
  214. }
  215. return arrArr;
  216. }
  217. //enum
  218. public static T StringToEnum<T>(string valueStr) where T : Enum
  219. {
  220. if(string.IsNullOrEmpty(valueStr))
  221. return (T)default;
  222. //先校验字符串是否为枚举值
  223. int intValue;
  224. if(int.TryParse(valueStr,out intValue))
  225. {
  226. if(Enum.IsDefined(typeof(T),intValue))
  227. return (T)Enum.ToObject(typeof(T),intValue);
  228. }
  229. //如果不是枚举值,当做枚举名处理
  230. try
  231. {
  232. T t = (T)Enum.Parse(typeof(T),valueStr);
  233. if(Enum.IsDefined(typeof(T),t))
  234. return t;
  235. }
  236. catch(Exception e)
  237. {
  238. Debug.LogError(e);
  239. }
  240. Debug.LogError(string.Format("解析枚举错误 {0} : {1}",typeof(T),valueStr));
  241. return (T)default;
  242. }
  243. public static T[] StringToEnumArray<T>(string valueStr,char splitSign = '|') where T : Enum
  244. {
  245. if(string.IsNullOrEmpty(valueStr))
  246. return null;
  247. string[] valueArr = valueStr.Split(splitSign);
  248. if(valueArr == null || valueArr.Length == 0)
  249. return null;
  250. T[] enumArr = new T[valueArr.Length];
  251. for(int i = 0; i < valueArr.Length; i++)
  252. {
  253. enumArr[i] = StringToEnum<T>(valueArr[i]);
  254. }
  255. return enumArr;
  256. }
  257. ////不支持泛型枚举序列化
  258. //public static EnumArr<T>[] StringToEnumArray2D<T>(string valueStr,char splitSign1 = '&',char splitSign2 = '|') where T : Enum
  259. //{
  260. // if(string.IsNullOrEmpty(valueStr))
  261. // return null;
  262. // string[] strArr1 = valueStr.Split(splitSign1);
  263. // if(strArr1.Length == 0)
  264. // return null;
  265. // EnumArr<T>[] arrArr = new EnumArr<T>[strArr1.Length];
  266. // for(int i = 0; i < strArr1.Length; i++)
  267. // {
  268. // arrArr[i] = new EnumArr<T>()
  269. // {
  270. // array = StringToEnumArray<T>(strArr1[i],splitSign2)
  271. // };
  272. // }
  273. // return arrArr;
  274. //}
  275. //vector2
  276. public static Vector2 StringToVector2(string valueStr,char splitSign = ',')
  277. {
  278. Vector2 value = Vector2.zero;
  279. if(!string.IsNullOrEmpty(valueStr))
  280. {
  281. string[] stringArray = valueStr.Split(splitSign);
  282. if(stringArray != null && stringArray.Length >= 2)
  283. {
  284. value.x = StringToFloat(stringArray[0]);
  285. value.y = StringToFloat(stringArray[1]);
  286. return value;
  287. }
  288. }
  289. Debug.LogWarning("String to Vector2 error");
  290. return value;
  291. }
  292. public static Vector2[] StringToVector2Array(string valueStr,char splitSign = '|')
  293. {
  294. if(string.IsNullOrEmpty(valueStr))
  295. return null;
  296. string[] stringArray = valueStr.Split(splitSign);
  297. if(stringArray == null || stringArray.Length == 0)
  298. return null;
  299. Vector2[] vector2s = new Vector2[stringArray.Length];
  300. for(int i = 0; i < stringArray.Length; i++)
  301. {
  302. vector2s[i] = StringToVector2(stringArray[i]);
  303. }
  304. return vector2s;
  305. }
  306. public static Vector2Arr[] StringToVector2Array2D(string valueStr,char splitSign1 = '&',char splitSign2 = '|')
  307. {
  308. if(string.IsNullOrEmpty(valueStr))
  309. return null;
  310. string[] strArr1 = valueStr.Split(splitSign1);
  311. if(strArr1.Length == 0)
  312. return null;
  313. Vector2Arr[] arrArr = new Vector2Arr[strArr1.Length];
  314. for(int i = 0; i < strArr1.Length; i++)
  315. {
  316. arrArr[i] = new Vector2Arr()
  317. {
  318. array = StringToVector2Array(strArr1[i],splitSign2)
  319. };
  320. }
  321. return arrArr;
  322. }
  323. //vector3
  324. public static Vector3 StringToVector3(string valueStr,char splitSign = ',')
  325. {
  326. Vector3 value = Vector3.zero;
  327. if(!string.IsNullOrEmpty(valueStr))
  328. {
  329. string[] stringArray = valueStr.Split(splitSign);
  330. if(stringArray.Length >= 3)
  331. {
  332. value.x = StringToFloat(stringArray[0]);
  333. value.y = StringToFloat(stringArray[1]);
  334. value.z = StringToFloat(stringArray[2]);
  335. return value;
  336. }
  337. }
  338. Debug.LogWarning("String to Vector3 error");
  339. return value;
  340. }
  341. public static Vector3[] StringToVector3Array(string valueStr,char splitSign = '|')
  342. {
  343. if(string.IsNullOrEmpty(valueStr))
  344. return null;
  345. string[] stringArray = valueStr.Split(splitSign);
  346. if(stringArray == null || stringArray.Length == 0)
  347. return null;
  348. Vector3[] vector3s = new Vector3[stringArray.Length];
  349. for(int i = 0; i < stringArray.Length; i++)
  350. {
  351. vector3s[i] = StringToVector3(stringArray[i]);
  352. }
  353. return vector3s;
  354. }
  355. public static Vector3Arr[] StringToVector3Array2D(string valueStr,char splitSign1 = '&',char splitSign2 = '|')
  356. {
  357. if(string.IsNullOrEmpty(valueStr))
  358. return null;
  359. string[] strArr1 = valueStr.Split(splitSign1);
  360. if(strArr1.Length == 0)
  361. return null;
  362. Vector3Arr[] arrArr = new Vector3Arr[strArr1.Length];
  363. for(int i = 0; i < strArr1.Length; i++)
  364. {
  365. arrArr[i] = new Vector3Arr()
  366. {
  367. array = StringToVector3Array(strArr1[i],splitSign2)
  368. };
  369. }
  370. return arrArr;
  371. }
  372. //vector2Int
  373. public static Vector2Int StringToVector2Int(string valueStr,char splitSign = ',')
  374. {
  375. Vector2Int value = Vector2Int.zero;
  376. if(!string.IsNullOrEmpty(valueStr))
  377. {
  378. string[] stringArray = valueStr.Split(splitSign);
  379. if(stringArray != null && stringArray.Length >= 2)
  380. {
  381. value.x = StringToInt(stringArray[0]);
  382. value.y = StringToInt(stringArray[1]);
  383. return value;
  384. }
  385. }
  386. Debug.LogWarning("String to Vector2Int error");
  387. return value;
  388. }
  389. public static Vector2Int[] StringToVector2IntArray(string valueStr,char splitSign = '|')
  390. {
  391. if(string.IsNullOrEmpty(valueStr))
  392. return null;
  393. string[] stringArray = valueStr.Split(splitSign);
  394. if(stringArray == null || stringArray.Length == 0)
  395. return null;
  396. Vector2Int[] vector2Ints = new Vector2Int[stringArray.Length];
  397. for(int i = 0; i < stringArray.Length; i++)
  398. {
  399. vector2Ints[i] = StringToVector2Int(stringArray[i]);
  400. }
  401. return vector2Ints;
  402. }
  403. public static Vector2IntArr[] StringToVector2IntArray2D(string valueStr,char splitSign1 = '&',char splitSign2 = '|')
  404. {
  405. if(string.IsNullOrEmpty(valueStr))
  406. return null;
  407. string[] strArr1 = valueStr.Split(splitSign1);
  408. if(strArr1.Length == 0)
  409. return null;
  410. Vector2IntArr[] arrArr = new Vector2IntArr[strArr1.Length];
  411. for(int i = 0; i < strArr1.Length; i++)
  412. {
  413. arrArr[i] = new Vector2IntArr()
  414. {
  415. array = StringToVector2IntArray(strArr1[i],splitSign2)
  416. };
  417. }
  418. return arrArr;
  419. }
  420. //vector3Int
  421. public static Vector3Int StringToVector3Int(string valueStr,char splitSign = ',')
  422. {
  423. Vector3Int value = Vector3Int.zero;
  424. if(!string.IsNullOrEmpty(valueStr))
  425. {
  426. string[] stringArray = valueStr.Split(splitSign);
  427. if(stringArray.Length >= 3)
  428. {
  429. value.x = StringToInt(stringArray[0]);
  430. value.y = StringToInt(stringArray[1]);
  431. value.z = StringToInt(stringArray[2]);
  432. return value;
  433. }
  434. }
  435. Debug.LogWarning("String to Vector3 error");
  436. return value;
  437. }
  438. public static Vector3Int[] StringToVector3IntArray(string valueStr,char splitSign = '|')
  439. {
  440. if(string.IsNullOrEmpty(valueStr))
  441. return null;
  442. string[] stringArray = valueStr.Split(splitSign);
  443. if(stringArray == null || stringArray.Length == 0)
  444. return null;
  445. Vector3Int[] vector3Ints = new Vector3Int[stringArray.Length];
  446. for(int i = 0; i < stringArray.Length; i++)
  447. {
  448. vector3Ints[i] = StringToVector3Int(stringArray[i]);
  449. }
  450. return vector3Ints;
  451. }
  452. public static Vector3IntArr[] StringToVector3IntArray2D(string valueStr,char splitSign1 = '&',char splitSign2 = '|')
  453. {
  454. if(string.IsNullOrEmpty(valueStr))
  455. return null;
  456. string[] strArr1 = valueStr.Split(splitSign1);
  457. if(strArr1.Length == 0)
  458. return null;
  459. Vector3IntArr[] arrArr = new Vector3IntArr[strArr1.Length];
  460. for(int i = 0; i < strArr1.Length; i++)
  461. {
  462. arrArr[i] = new Vector3IntArr()
  463. {
  464. array = StringToVector3IntArray(strArr1[i],splitSign2)
  465. };
  466. }
  467. return arrArr;
  468. }
  469. //color
  470. public static Color StringToColor(string valueStr,char splitSign = ',')
  471. {
  472. if(string.IsNullOrEmpty(valueStr))
  473. return Color.white;
  474. string[] stringArray = valueStr.Split(splitSign);
  475. if(stringArray.Length < 3)
  476. return Color.white;
  477. Color color = new Color()
  478. {
  479. r = StringToFloat(stringArray[0]),
  480. g = StringToFloat(stringArray[1]),
  481. b = StringToFloat(stringArray[2]),
  482. a = stringArray.Length < 4 ? 1 : StringToFloat(stringArray[3])
  483. };
  484. return color;
  485. }
  486. public static Color32 StringToColor32(string valueStr,char splitSign = ',')
  487. {
  488. if(string.IsNullOrEmpty(valueStr))
  489. return Color.white;
  490. string[] stringArray = valueStr.Split(splitSign);
  491. if(stringArray.Length < 3)
  492. return Color.white;
  493. Color32 color = new Color32()
  494. {
  495. r = StringToByte(stringArray[0]),
  496. g = StringToByte(stringArray[1]),
  497. b = StringToByte(stringArray[2]),
  498. a = stringArray.Length < 4 ? (byte)1 : StringToByte(stringArray[3])
  499. };
  500. return color;
  501. }
  502. public static Color[] StringToColorArray(string valueStr,char splitSign = '|')
  503. {
  504. if(string.IsNullOrEmpty(valueStr))
  505. return null;
  506. string[] stringArray = valueStr.Split(splitSign);
  507. if(stringArray == null || stringArray.Length == 0)
  508. return null;
  509. Color[] colors = new Color[stringArray.Length];
  510. for(int i = 0; i < stringArray.Length; i++)
  511. {
  512. colors[i] = StringToColor(stringArray[i]);
  513. }
  514. return colors;
  515. }
  516. public static Color32[] StringToColor32Array(string valueStr,char splitSign = '|')
  517. {
  518. if(string.IsNullOrEmpty(valueStr))
  519. return null;
  520. string[] stringArray = valueStr.Split(splitSign);
  521. if(stringArray == null || stringArray.Length == 0)
  522. return null;
  523. Color32[] colors = new Color32[stringArray.Length];
  524. for(int i = 0; i < stringArray.Length; i++)
  525. {
  526. colors[i] = StringToColor32(stringArray[i]);
  527. }
  528. return colors;
  529. }
  530. public static ColorArr[] StringToColorArray2D(string valueStr,char splitSign1 = '&',char splitSign2 = '|')
  531. {
  532. if(string.IsNullOrEmpty(valueStr))
  533. return null;
  534. string[] strArr1 = valueStr.Split(splitSign1);
  535. if(strArr1.Length == 0)
  536. return null;
  537. ColorArr[] arrArr = new ColorArr[strArr1.Length];
  538. for(int i = 0; i < strArr1.Length; i++)
  539. {
  540. arrArr[i] = new ColorArr()
  541. {
  542. array = StringToColorArray(strArr1[i],splitSign2)
  543. };
  544. }
  545. return arrArr;
  546. }
  547. public static Color32Arr[] StringToColor32Array2D(string valueStr,char splitSign1 = '&',char splitSign2 = '|')
  548. {
  549. if(string.IsNullOrEmpty(valueStr))
  550. return null;
  551. string[] strArr1 = valueStr.Split(splitSign1);
  552. if(strArr1.Length == 0)
  553. return null;
  554. Color32Arr[] arrArr = new Color32Arr[strArr1.Length];
  555. for(int i = 0; i < strArr1.Length; i++)
  556. {
  557. arrArr[i] = new Color32Arr()
  558. {
  559. array = StringToColor32Array(strArr1[i],splitSign2)
  560. };
  561. }
  562. return arrArr;
  563. }
  564. #endregion
  565. #region MyRegion
  566. public static string GetRandomString(int length)
  567. {
  568. StringBuilder builder = new StringBuilder();
  569. string abc = "abcdefghijklmnopqrstuvwxyzo0123456789QWERTYUIOPASDFGHJKLZXCCVBMN";
  570. for(int i = 0; i < length; i++)
  571. {
  572. builder.Append(abc[UnityEngine.Random.Range(0,abc.Length - 1)]);
  573. }
  574. return builder.ToString();
  575. }
  576. public static string Join<T>(T[] arr,string join = ",")
  577. {
  578. if(arr == null || arr.Length == 0)
  579. return null;
  580. StringBuilder builder = new StringBuilder();
  581. for(int i = 0; i < arr.Length; i++)
  582. {
  583. builder.Append(arr[i]);
  584. if(i < arr.Length - 1)
  585. builder.Append(join);
  586. }
  587. return builder.ToString();
  588. }
  589. /// <summary>
  590. /// 中文逗号转英文逗号
  591. /// </summary>
  592. /// <param name="input"></param>
  593. /// <returns></returns>
  594. public static string ToDBC(string input)
  595. {
  596. char[] c = input.ToCharArray();
  597. for(int i = 0; i < c.Length; i++)
  598. {
  599. if(c[i] == 12288)
  600. {
  601. c[i] = (char)32;
  602. continue;
  603. }
  604. if(c[i] > 65280 && c[i] < 65375)
  605. c[i] = (char)(c[i] - 65248);
  606. }
  607. return new string(c);
  608. }
  609. /// <summary>
  610. /// 字符转 ascii 码
  611. /// </summary>
  612. /// <param name="character"></param>
  613. /// <returns></returns>
  614. public static int Asc(string character)
  615. {
  616. if(character.Length == 1)
  617. {
  618. System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
  619. int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];
  620. return (intAsciiCode);
  621. }
  622. Debug.LogError("Character is not valid.");
  623. return -1;
  624. }
  625. /// <summary>
  626. /// ascii码转字符
  627. /// </summary>
  628. /// <param name="asciiCode"></param>
  629. /// <returns></returns>
  630. public static string Chr(int asciiCode)
  631. {
  632. if(asciiCode >= 0 && asciiCode <= 255)
  633. {
  634. System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
  635. byte[] byteArray = new byte[] { (byte)asciiCode };
  636. string strCharacter = asciiEncoding.GetString(byteArray);
  637. return (strCharacter);
  638. }
  639. Debug.LogError("ASCII Code is not valid.");
  640. return string.Empty;
  641. }
  642. /// <summary>
  643. /// 过滤掉表情符号
  644. /// </summary>
  645. /// <returns>The emoji.</returns>
  646. /// <param name="str">String.</param>
  647. public static string FilterEmoji(string str)
  648. {
  649. List<string> patten = new List<string>() { @"\p{Cs}",@"\p{Co}",@"\p{Cn}",@"[\u2702-\u27B0]" };
  650. for(int i = 0; i < patten.Count; i++)
  651. {
  652. str = Regex.Replace(str,patten[i],"");//屏蔽emoji
  653. }
  654. return str;
  655. }
  656. /// <summary>
  657. /// 过滤掉表情符号
  658. /// </summary>
  659. /// <returns>The emoji.</returns>
  660. /// <param name="str">String.</param>
  661. public static bool IsFilterEmoji(string str)
  662. {
  663. bool bEmoji = false;
  664. List<string> patten = new List<string>() { @"\p{Cs}",@"\p{Co}",@"\p{Cn}",@"[\u2702-\u27B0]" };
  665. for(int i = 0; i < patten.Count; i++)
  666. {
  667. bEmoji = Regex.IsMatch(str,patten[i]);
  668. if(bEmoji)
  669. {
  670. break;
  671. }
  672. }
  673. return bEmoji;
  674. }
  675. #endregion
  676. #region StringObjectDictionaryExtensions
  677. /// <summary>
  678. /// 针对字典中包含以下键值进行结构:mctid0=xxx;mccount0=1,mctid1=kn2,mccount=2。将其前缀去掉,数字后缀变为键,如{后缀,(去掉前后缀的键,值)},注意后缀可能是空字符串即没有后缀
  679. /// </summary>
  680. /// <param name="dic"></param>
  681. /// <param name="prefix">前缀,可以是空引用或空字符串,都表示没有前缀。</param>
  682. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  683. public static IEnumerable<IGrouping<string,(string, object)>> GetValuesWithoutPrefix(this IReadOnlyDictionary<string,object> dic,string prefix = null)
  684. {
  685. //prefix ??= string.Empty;
  686. prefix = prefix ?? string.Empty;
  687. var coll = from tmp in dic.Where(c => c.Key.StartsWith(prefix)) //仅针对指定前缀的键值
  688. let p3 = tmp.Key.Get3Segment(prefix)
  689. group (p3.Item2, tmp.Value) by p3.Item3;
  690. return coll;
  691. }
  692. /// <summary>
  693. /// 分解字符串为三段,前缀,词根,数字后缀(字符串形式)。
  694. /// </summary>
  695. /// <param name="str"></param>
  696. /// <param name="prefix">前缀,可以是空引用或空字符串,都表示没有前缀。</param>
  697. /// <returns></returns>
  698. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  699. public static (string, string, string) Get3Segment(this string str,string prefix = null)
  700. {
  701. //prefix ??= string.Empty;
  702. prefix = prefix ?? string.Empty;
  703. //最后十进制数字尾串的长度
  704. int suffixLen = Enumerable.Reverse(str).TakeWhile(c => char.IsDigit(c)).Count();
  705. //获取十进制数字后缀
  706. //string suufix = str[^suffixLen..]; //^suffixLen:倒序下标;suffixLen..:从指定位置开始直到末尾
  707. string suufix = str.Substring(str.Length - suffixLen);
  708. //return (prefix, str[prefix.Length..^suufix.Length], suufix);
  709. string middle = str.Substring(prefix.Length,str.Length - prefix.Length - suufix.Length);
  710. return (prefix, middle, suufix);
  711. }
  712. #endregion
  713. }