StringEx.cs 4.4 KB

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