using System; using System.Collections.Generic; using System.Data; using System.IO; using System.IO.Compression; using System.Reflection; using System.Text; using System.Linq; using System.Security.Cryptography; namespace WWPipeLine.Commons { public class StringEx { ///<summary> ///生成随机字符串 ///</summary> ///<param name="length">目标字符串的长度</param> ///<param name="useNum">是否包含数字,1=包含,默认为包含</param> ///<param name="useLow">是否包含小写字母,1=包含,默认为包含</param> ///<param name="useUpp">是否包含大写字母,1=包含,默认为包含</param> ///<param name="useSpe">是否包含特殊字符,1=包含,默认为不包含</param> ///<param name="custom">要包含的自定义字符,直接输入要包含的字符列表</param> ///<returns>指定长度的随机字符串</returns> public static string GetRandomString(int length, bool useNum, bool useLow, bool useUpp, bool useSpe, string custom) { byte[] b = new byte[4]; new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(b); Random r = new Random(BitConverter.ToInt32(b, 0)); string s = null, str = custom; if (useNum == true) { str += "0123456789"; } if (useLow == true) { str += "abcdefghijklmnopqrstuvwxyz"; } if (useUpp == true) { str += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; } if (useSpe == true) { str += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; } for (int i = 0; i < length; i++) { s += str.Substring(r.Next(0, str.Length - 1), 1); } return s; } /// <summary> /// 字符串压缩 /// </summary> /// <param name="strSource"></param> /// <returns></returns> public static byte[] Compress(byte[] data) { try { MemoryStream ms = new MemoryStream(); GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true); zip.Write(data, 0, data.Length); zip.Close(); byte[] buffer = new byte[ms.Length]; ms.Position = 0; ms.Read(buffer, 0, buffer.Length); ms.Close(); return buffer; } catch (Exception e) { throw new Exception(e.Message); } } /// <summary> /// 字符串解压缩 /// </summary> /// <param name="strSource"></param> /// <returns></returns> public static byte[] Decompress(byte[] data) { try { MemoryStream ms = new MemoryStream(data); GZipStream zip = new GZipStream(ms, CompressionMode.Decompress, true); MemoryStream msreader = new MemoryStream(); byte[] buffer = new byte[0x1000]; while (true) { int reader = zip.Read(buffer, 0, buffer.Length); if (reader <= 0) { break; } msreader.Write(buffer, 0, reader); } zip.Close(); ms.Close(); msreader.Position = 0; buffer = msreader.ToArray(); msreader.Close(); return buffer; } catch (Exception e) { throw new Exception(e.Message); } } public static string CompressString(string str) { string compressString = ""; byte[] compressBeforeByte = Encoding.GetEncoding("UTF-8").GetBytes(str); byte[] compressAfterByte = Compress(compressBeforeByte); //compressString = Encoding.GetEncoding("UTF-8").GetString(compressAfterByte); compressString = Convert.ToBase64String(compressAfterByte); return compressString; } public static string DecompressString(string str) { string compressString = ""; //byte[] compressBeforeByte = Encoding.GetEncoding("UTF-8").GetBytes(str); byte[] compressBeforeByte = Convert.FromBase64String(str); byte[] compressAfterByte = Decompress(compressBeforeByte); compressString = Encoding.GetEncoding("UTF-8").GetString(compressAfterByte); return compressString; } public static string[] setToLower(List<string> listStr) { if (listStr is null) return new string[] { "" }; List<string> strList = new List<string>(); foreach (string s in listStr.ToArray()) { strList.Add(s.ToLower()); } return strList.ToArray(); } public static string[] setToLower(string[] listStr) { if (listStr is null) return new string[] { "" }; List<string> strList = new List<string>(); foreach (string s in listStr) { strList.Add(s.ToLower()); } return strList.ToArray(); } public static List<string> setToLowerList(List<string> listStr) { List<string> strList = new List<string>(); foreach (string s in listStr.ToArray()) { strList.Add(s.ToLower()); } return strList; } } }