Split.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6. /// <summary>
  7. /// 字符串分割相关工具集
  8. /// </summary>
  9. public partial class Utils
  10. {
  11. /// <summary>
  12. /// 序列化指定目标, 逗号分隔符
  13. /// </summary>
  14. /// <param name="buff">目标数据</param>
  15. /// <returns>序列化后字符串</returns>
  16. public static string SeralizeToDotString(int[] buff)
  17. {
  18. string tmp = string.Empty;
  19. for (int i = 0; i < buff.Length; ++i)
  20. {
  21. tmp += buff[i];
  22. tmp += ",";
  23. }
  24. return tmp;
  25. }
  26. /// <summary>
  27. /// 序列化指定目标, 逗号分隔符
  28. /// </summary>
  29. /// <param name="buff">目标数据</param>
  30. /// <returns>序列化后字符串</returns>
  31. public static string SeralizeToDotString(List<int> buff)
  32. {
  33. string tmp = string.Empty;
  34. for (int i = 0; i < buff.Count; ++i)
  35. {
  36. tmp += buff[i];
  37. tmp += ",";
  38. }
  39. return tmp;
  40. }
  41. /// <summary>
  42. /// 反序列化逗号分割字符串
  43. /// </summary>
  44. /// <param name="from">字符串</param>
  45. /// <param name="dst">目标缓冲区</param>
  46. public static void UnserializeDotString(string from, ref int[] dst)
  47. {
  48. string[] split = from.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  49. for (int i = 0; i < split.Length; ++i)
  50. {
  51. dst[i] = Convert.ToInt32(split[i]);
  52. }
  53. }
  54. /// <summary>
  55. /// 反序列化逗号分割字符串
  56. /// </summary>
  57. /// <param name="from">字符串</param>
  58. /// <param name="dst">目标缓冲区</param>
  59. public static void UnserializeDotString(string from, ref List<int> dst)
  60. {
  61. string[] split = from.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  62. for (int i = 0; i < split.Length; ++i)
  63. {
  64. dst.Add(Convert.ToInt32(split[i]));
  65. }
  66. }
  67. /// <summary>
  68. /// 反序列化逗号分割字符串
  69. /// </summary>
  70. /// <param name="from">字符串</param>
  71. /// <returns>反序列化后列表</returns>
  72. public static List<int> UnserializeDotString(string from)
  73. {
  74. List<int> dst = new List<int>();
  75. string[] split = from.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  76. for (int i = 0; i < split.Length; ++i)
  77. {
  78. dst.Add(Convert.ToInt32(split[i]));
  79. }
  80. return dst;
  81. }
  82. /// <summary>
  83. /// 反序列化逗号分割字符串
  84. /// </summary>
  85. /// <param name="from">字符串</param>
  86. /// <returns>反序列化后列表</returns>
  87. public static List<double> UnserializeDotStringToDouble(string from)
  88. {
  89. List<double> dst = new List<double>();
  90. string[] split = from.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  91. for (int i = 0; i < split.Length; ++i)
  92. {
  93. dst.Add(double.Parse(split[i]));
  94. }
  95. return dst;
  96. }
  97. /// <summary>
  98. /// 反序列化逗号分割字符串
  99. /// </summary>
  100. /// <param name="from">字符串</param>
  101. /// <returns>反序列化后列表</returns>
  102. public static List<string> UnserializeDotStringToString(string from)
  103. {
  104. List<string> dst = new List<string>();
  105. string[] split = from.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  106. for (int i = 0; i < split.Length; ++i)
  107. {
  108. dst.Add(split[i]);
  109. }
  110. return dst;
  111. }
  112. }