StringUtil.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. public class StringUtil
  5. {
  6. public static string[] Split(string str, char c)
  7. {
  8. if(Empty(str))
  9. return new string[0];
  10. return str.Split(new char[]{c});
  11. }
  12. public static int[] SplitToInt(string str, char c)
  13. {
  14. if(Empty(str))
  15. return new int[0];
  16. string[] strArr = Split(str, c);
  17. int[] intArr = new int[strArr.Length];
  18. for(int i=0; i<strArr.Length; i++)
  19. {
  20. intArr[i] = ToInt(strArr[i]);
  21. }
  22. return intArr;
  23. }
  24. public static float[] SplitToFloat(string str, char c)
  25. {
  26. if(Empty(str))
  27. return new float[0];
  28. string[] strArr = Split(str, c);
  29. float[] floatArr = new float[strArr.Length];
  30. for(int i=0; i<strArr.Length; i++)
  31. {
  32. floatArr[i] = ToFloat(strArr[i]);
  33. }
  34. return floatArr;
  35. }
  36. public static bool Empty(string str)
  37. {
  38. return str == null || str == "";
  39. }
  40. public static string FillZero(string str, int count)
  41. {
  42. str = "0000000000000000"+str;
  43. str = str.Substring(str.Length-count, count);
  44. return str;
  45. }
  46. public static string FillZero(int value, int count)
  47. {
  48. return FillZero(value.ToString(), count);
  49. }
  50. public static int ToInt(string str)
  51. {
  52. if(StringUtil.Empty(str))
  53. {
  54. return 0;
  55. }
  56. return int.Parse(str);
  57. }
  58. public static float ToFloat(string str)
  59. {
  60. if(StringUtil.Empty(str))
  61. {
  62. return 0;
  63. }
  64. return float.Parse(str);
  65. }
  66. public static bool ToBool(string str)
  67. {
  68. str = str.ToLower();
  69. if(str == "true")
  70. {
  71. return true;
  72. }
  73. else if(str == "false")
  74. {
  75. return false;
  76. }
  77. int value = ToInt(str);
  78. if(value > 0)
  79. return true;
  80. return false;
  81. }
  82. public static string Join(List<string> list, string separator)
  83. {
  84. string str = "";
  85. for (int i = 0; i < list.Count; i++)
  86. {
  87. str += list[i];
  88. if (i < list.Count - 1)
  89. str += separator;
  90. }
  91. return str;
  92. }
  93. }