StringEx.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.IO;
  5. using System.IO.Compression;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Linq;
  9. namespace WWPipeLine.Commons
  10. {
  11. public class StringEx
  12. {
  13. ///<summary>
  14. ///生成随机字符串
  15. ///</summary>
  16. ///<param name="length">目标字符串的长度</param>
  17. ///<param name="useNum">是否包含数字,1=包含,默认为包含</param>
  18. ///<param name="useLow">是否包含小写字母,1=包含,默认为包含</param>
  19. ///<param name="useUpp">是否包含大写字母,1=包含,默认为包含</param>
  20. ///<param name="useSpe">是否包含特殊字符,1=包含,默认为不包含</param>
  21. ///<param name="custom">要包含的自定义字符,直接输入要包含的字符列表</param>
  22. ///<returns>指定长度的随机字符串</returns>
  23. public static string GetRandomString(int length, bool useNum, bool useLow, bool useUpp, bool useSpe, string custom)
  24. {
  25. byte[] b = new byte[4];
  26. new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(b);
  27. Random r = new Random(BitConverter.ToInt32(b, 0));
  28. string s = null, str = custom;
  29. if (useNum == true) { str += "0123456789"; }
  30. if (useLow == true) { str += "abcdefghijklmnopqrstuvwxyz"; }
  31. if (useUpp == true) { str += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; }
  32. if (useSpe == true) { str += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; }
  33. for (int i = 0; i < length; i++)
  34. {
  35. s += str.Substring(r.Next(0, str.Length - 1), 1);
  36. }
  37. return s;
  38. }
  39. /// <summary>
  40. /// 字符串压缩
  41. /// </summary>
  42. /// <param name="strSource"></param>
  43. /// <returns></returns>
  44. public static byte[] Compress(byte[] data)
  45. {
  46. try
  47. {
  48. MemoryStream ms = new MemoryStream();
  49. GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true);
  50. zip.Write(data, 0, data.Length);
  51. zip.Close();
  52. byte[] buffer = new byte[ms.Length];
  53. ms.Position = 0;
  54. ms.Read(buffer, 0, buffer.Length);
  55. ms.Close();
  56. return buffer;
  57. }
  58. catch (Exception e)
  59. {
  60. throw new Exception(e.Message);
  61. }
  62. }
  63. /// <summary>
  64. /// 字符串解压缩
  65. /// </summary>
  66. /// <param name="strSource"></param>
  67. /// <returns></returns>
  68. public static byte[] Decompress(byte[] data)
  69. {
  70. try
  71. {
  72. MemoryStream ms = new MemoryStream(data);
  73. GZipStream zip = new GZipStream(ms, CompressionMode.Decompress, true);
  74. MemoryStream msreader = new MemoryStream();
  75. byte[] buffer = new byte[0x1000];
  76. while (true)
  77. {
  78. int reader = zip.Read(buffer, 0, buffer.Length);
  79. if (reader <= 0)
  80. {
  81. break;
  82. }
  83. msreader.Write(buffer, 0, reader);
  84. }
  85. zip.Close();
  86. ms.Close();
  87. msreader.Position = 0;
  88. buffer = msreader.ToArray();
  89. msreader.Close();
  90. return buffer;
  91. }
  92. catch (Exception e)
  93. {
  94. throw new Exception(e.Message);
  95. }
  96. }
  97. public static string CompressString(string str)
  98. {
  99. string compressString = "";
  100. byte[] compressBeforeByte = Encoding.GetEncoding("UTF-8").GetBytes(str);
  101. byte[] compressAfterByte = Compress(compressBeforeByte);
  102. //compressString = Encoding.GetEncoding("UTF-8").GetString(compressAfterByte);
  103. compressString = Convert.ToBase64String(compressAfterByte);
  104. return compressString;
  105. }
  106. public static string DecompressString(string str)
  107. {
  108. string compressString = "";
  109. //byte[] compressBeforeByte = Encoding.GetEncoding("UTF-8").GetBytes(str);
  110. byte[] compressBeforeByte = Convert.FromBase64String(str);
  111. byte[] compressAfterByte = Decompress(compressBeforeByte);
  112. compressString = Encoding.GetEncoding("UTF-8").GetString(compressAfterByte);
  113. return compressString;
  114. }
  115. public static string[] setToLower(List<string> listStr)
  116. {
  117. if (listStr is null) return new string[] { "" };
  118. List<string> strList = new List<string>();
  119. foreach (string s in listStr.ToArray())
  120. {
  121. strList.Add(s.ToLower());
  122. }
  123. return strList.ToArray();
  124. }
  125. public static string[] setToLower(string[] listStr)
  126. {
  127. if (listStr is null) return new string[] { "" };
  128. List<string> strList = new List<string>();
  129. foreach (string s in listStr)
  130. {
  131. strList.Add(s.ToLower());
  132. }
  133. return strList.ToArray();
  134. }
  135. public static List<string> setToLowerList(List<string> listStr)
  136. {
  137. List<string> strList = new List<string>();
  138. foreach (string s in listStr.ToArray())
  139. {
  140. strList.Add(s.ToLower());
  141. }
  142. return strList;
  143. }
  144. }
  145. }