StringUtil.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System.Text.RegularExpressions;
  6. public class StringUtil
  7. {
  8. public static bool isIpString(string str)
  9. {
  10. string[] tempList = str.Split('.');
  11. if (tempList == null || tempList.Length != 4) return false;
  12. for (int idx = 0; idx < tempList.Length; idx++)
  13. {
  14. string temp = tempList[idx];
  15. int iVal = 0;
  16. if (!int.TryParse(temp,out iVal))
  17. return false;
  18. }
  19. return true;
  20. }
  21. public static string[] split(string str,char sperator)
  22. {
  23. if (str == null || str.Length == 0)
  24. return null;
  25. return str.Split(sperator);
  26. }
  27. public static int[] split2Int(string str,char sperator)
  28. {
  29. string[] strList = split(str, sperator);
  30. if (strList == null || strList.Length == 0)
  31. return null;
  32. int[] intList = new int[strList.Length];
  33. for(int idx =0;idx < strList.Length;idx++)
  34. {
  35. string temp = strList[idx];
  36. int iVal;
  37. if(int.TryParse(temp, out iVal))
  38. {
  39. intList[idx] = iVal;
  40. }else
  41. {
  42. float fVal;
  43. if(float.TryParse(temp, out fVal))
  44. {
  45. intList[idx] = (int)fVal;
  46. }
  47. else
  48. {
  49. intList[idx] = 0;
  50. }
  51. }
  52. }
  53. return intList;
  54. }
  55. public static float[] split2Float(string str, char sperator)
  56. {
  57. string[] strList = split(str, sperator);
  58. if (strList == null || strList.Length == 0)
  59. return null;
  60. float[] floatList = new float[strList.Length];
  61. for (int idx = 0; idx < strList.Length; idx++)
  62. {
  63. string temp = strList[idx];
  64. float fVal;
  65. if (float.TryParse(temp, out fVal))
  66. {
  67. floatList[idx] = fVal;
  68. }
  69. else
  70. {
  71. floatList[idx] = 0;
  72. }
  73. }
  74. return floatList;
  75. }
  76. public static List<int> convert2IntList(string str, char seperator)
  77. {
  78. string[] strList = split(str, seperator);
  79. if (strList == null || strList.Length == 0)
  80. return null;
  81. List<int> intList = new List<int>(strList.Length);
  82. for (int idx = 0; idx < strList.Length; idx++)
  83. {
  84. string temp = strList[idx];
  85. int fVal;
  86. if (int.TryParse(temp, out fVal))
  87. {
  88. intList.Add(fVal);
  89. }
  90. else
  91. {
  92. intList.Add(0);
  93. }
  94. }
  95. return intList;
  96. }
  97. public static Vector3 convertVector3(string str)
  98. {
  99. if (string.IsNullOrEmpty(str))
  100. return Vector3.zero;
  101. string[] strList = split(str, ';');
  102. if (strList == null || strList.Length !=3)
  103. return Vector3.zero;
  104. float x, y, z;
  105. float.TryParse(strList[0],out x);
  106. float.TryParse(strList[1],out y);
  107. float.TryParse(strList[2],out z);
  108. return new Vector3(x,y,z);
  109. }
  110. public static Vector2 convertVector2(string str)
  111. {
  112. if (string.IsNullOrEmpty(str))
  113. return Vector2.zero;
  114. string[] strList = split(str, ';');
  115. if (strList == null || strList.Length != 2)
  116. return Vector2.zero;
  117. float x, y;
  118. float.TryParse(strList[0], out x);
  119. float.TryParse(strList[1], out y);
  120. return new Vector2(x,y);
  121. }
  122. public static string ConvertVector2Str(Vector3 v)
  123. {
  124. return v.x + ";" + v.y + ";" + v.z;
  125. }
  126. /// <summary>
  127. /// 获取obj中的vector3数据
  128. /// </summary>
  129. /// <param name="jsonObj"></param>
  130. /// <param name="name"></param>
  131. /// <returns></returns>
  132. public static Vector3 GetVector3Field(Dictionary<object, object> jsonObj,string name)
  133. {
  134. if(jsonObj.ContainsKey(name))
  135. {
  136. List<object> objList = (List<object>)jsonObj[name];
  137. if (objList == null || objList.Count < 3)
  138. return Vector3.zero;
  139. float x, y, z;
  140. float.TryParse(objList[0].ToString(),out x);
  141. float.TryParse(objList[1].ToString(),out y);
  142. float.TryParse(objList[2].ToString(), out z);
  143. return new Vector3(x,y,z);
  144. }
  145. return Vector3.zero;
  146. }
  147. public static Vector2 GetVector2Field(Dictionary<object, object> jsonObj, string name)
  148. {
  149. if (jsonObj.ContainsKey(name))
  150. {
  151. List<object> objList = (List<object>)jsonObj[name];
  152. if (objList == null || objList.Count < 2)
  153. return Vector2.zero;
  154. float x, y;
  155. float.TryParse(objList[0].ToString(), out x);
  156. float.TryParse(objList[1].ToString(), out y);
  157. return new Vector2(x,y);
  158. }
  159. return Vector2.zero;
  160. }
  161. public static string PrintLong(ulong uid)
  162. {
  163. return uid.ToString();
  164. }
  165. /// <summary>
  166. /// 获取字符串在text中的长度
  167. /// </summary>
  168. /// <param name="text"></param>
  169. /// <param name="str"></param>
  170. /// <returns></returns>
  171. public static int GetTextLeng(Text text, string str = null)
  172. {
  173. Font mFont = text.font;
  174. string mStr = string.IsNullOrEmpty(str) ? text.text : str;
  175. mFont.RequestCharactersInTexture(mStr, text.fontSize, text.fontStyle);
  176. char[] charArr = mStr.ToCharArray();
  177. int totalTextLeng = 0;
  178. CharacterInfo character = new CharacterInfo();
  179. for (int i = 0; i < charArr.Length; i++)
  180. {
  181. mFont.GetCharacterInfo(charArr[i], out character, text.fontSize);
  182. totalTextLeng += character.advance;
  183. }
  184. return totalTextLeng;
  185. }
  186. public static string TrimEdgeSpace(string str)
  187. {
  188. return str.TrimStart(' ').TrimEnd(' ');
  189. }
  190. public static string Trim(string str)
  191. {
  192. return str.Trim();
  193. }
  194. /// <summary>
  195. /// 截取字符串,这里有一个约定,_count这个数量,一个中文字算2,其他的字符算1,这是迎合策划的要求产生的
  196. /// 这个函数只能截取0-_count,比如:"aaa哈哈",如果_count == 4,截取的结果是"aaa哈",但是_count == 5也同样是这个结果
  197. /// </summary>
  198. /// <param name="_str"></param>
  199. /// <param name="_count"></param>
  200. /// <returns></returns>
  201. public static string CutOutString(string _str, int _count)
  202. {
  203. int _totalCount = GetStringByteLength(_str);
  204. if (_totalCount <= _count)
  205. {
  206. return _str;
  207. }
  208. else
  209. {
  210. string _newStr = "";
  211. string _newStrL = "";
  212. var _charArr = _str.ToCharArray();
  213. int _index = 0;
  214. while (GetStringByteLength(_newStrL) < _count || _index > _charArr.Length)
  215. {
  216. _newStrL += _charArr[_index].ToString();
  217. _newStr = _newStrL;
  218. _index++;
  219. }
  220. return _newStr;
  221. }
  222. }
  223. /// <summary>
  224. /// 获取一个字符串的字节数
  225. /// </summary>
  226. /// <param name="_str"></param>
  227. /// <returns></returns>
  228. public static int GetStringByteLength(string _str)
  229. {
  230. if (string.IsNullOrEmpty(_str))
  231. return 0;
  232. else
  233. {
  234. var _chrArray = _str.ToCharArray();
  235. int _cnCount = 0;
  236. foreach (var _item in _chrArray)
  237. {
  238. bool _isCn = JudgeCharType(_item) == 1;
  239. _cnCount += _isCn ? 1 : 0;
  240. }
  241. return _cnCount * 2 + (_chrArray.Length - _cnCount);
  242. }
  243. }
  244. /*****判断字符传是否是由中文英文和数组组成的start*****/
  245. /// <summary>
  246. /// 判断字符的语言是什么类型
  247. /// 0 --当前没有判断的类型
  248. /// 1 --中文
  249. /// 2 --英文
  250. /// 3 --数字
  251. /// </summary>
  252. /// <param name="_chr"></param>
  253. /// <returns></returns>
  254. public static int JudgeCharType(char _chr)
  255. {
  256. string _str = _chr.ToString();
  257. int _match = 0;
  258. if (Regex.IsMatch(_str, "[\u4e00-\u9fa5]"))
  259. _match = 1;
  260. else if (Regex.IsMatch(_str, "[a-zA-Z]"))
  261. _match = 2;
  262. else if (Regex.IsMatch(_str, "[0-9]"))
  263. _match = 3;
  264. return _match;
  265. }
  266. /// <summary>
  267. /// 判断字符串是否是由中文英文和数字组成的
  268. /// </summary>
  269. /// <param name="_str"></param>
  270. /// <returns></returns>
  271. public static bool JudgeString_CN_EN_NUM(string _str)
  272. {
  273. if (string.IsNullOrEmpty(_str))
  274. {
  275. Debug.LogErrorFormat("null or empty string:{0}", _str);
  276. return false;
  277. }
  278. bool _isTarget = true;
  279. var _chrArray = _str.ToCharArray();
  280. foreach (var _item in _chrArray)
  281. {
  282. _isTarget = JudgeCharType(_item) != 0;
  283. if (_isTarget == false)
  284. break;
  285. }
  286. return _isTarget;
  287. }
  288. /*****判断字符传是否是由中文英文和数组组成的end*****/
  289. //
  290. public static string FilterEmoji(string str)
  291. {
  292. str = Regex.Replace(str, @"\p{Cs}", "");
  293. str = Regex.Replace(str, @"\p{Co}", "");
  294. str = Regex.Replace(str, @"\p{Cn}", "");
  295. str = Regex.Replace(str, @"[\u2702-\u27B0]", "");
  296. return str;
  297. }
  298. }