StringUtil.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Text;
  3. using System.Text.RegularExpressions;
  4. using UnityEngine;
  5. namespace GFGGame
  6. {
  7. public class StringUtil
  8. {
  9. private static int[] secPosValue = { 1601, 1637, 1833, 2078, 2274, 2302, 2433, 2594, 2787, 3106, 3212, 3472, 3635, 3722, 3730, 3858, 4327, 4086, 4390, 4558, 4684, 4925, 5249, 9999 };
  10. private static char[] firstLetter = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 't', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'w', 'x', 'y', 'z' };
  11. /// <summary>
  12. /// 设置指定文本的颜色
  13. /// </summary>
  14. /// <param name="fmt"></param>
  15. /// <param name="color"></param>
  16. /// <returns></returns>
  17. public static string GetColorText(string fmt, string color)
  18. {
  19. string str = string.Format("[color={0}]{1}[/color]", color, fmt);
  20. return str;
  21. }
  22. public static char GetPinYin(string str)
  23. {
  24. if (Convert.ToChar(str[0]) >= 0 && Convert.ToChar(str[0]) < 256)
  25. return str[0];
  26. else
  27. return GetFirstLetter(str[0].ToString());
  28. }
  29. //根据汉字拼音排序得到首字母
  30. private static char GetFirstLetter(string str)
  31. {
  32. string posValue = StringToPos(str);
  33. for (int i = 0; i < secPosValue.Length - 1; i++)
  34. {
  35. if (int.Parse(posValue) >= secPosValue[i] && int.Parse(posValue) < secPosValue[i + 1])
  36. {
  37. return firstLetter[i];
  38. }
  39. }
  40. return 'z';
  41. }
  42. //获取一个字符的区位码
  43. private static string StringToPos(string value)
  44. {
  45. string posValue = string.Empty;
  46. byte[] bytes = System.Text.Encoding.GetEncoding("GB2312").GetBytes(value);
  47. string lowCode = (bytes[0] - 160).ToString(); //取出低字节编码内容(两位16进制)
  48. if (lowCode.Length == 1)
  49. lowCode = "0" + lowCode;
  50. string hightCode = (bytes[1] - 160).ToString();//取出高字节编码内容(两位16进制)
  51. if (hightCode.Length == 1)
  52. hightCode = "0" + hightCode;
  53. posValue += (lowCode + hightCode);//加入到字符串中,
  54. return posValue;
  55. }
  56. }
  57. }