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
{
///
///生成随机字符串
///
///目标字符串的长度
///是否包含数字,1=包含,默认为包含
///是否包含小写字母,1=包含,默认为包含
///是否包含大写字母,1=包含,默认为包含
///是否包含特殊字符,1=包含,默认为不包含
///要包含的自定义字符,直接输入要包含的字符列表
///指定长度的随机字符串
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;
}
///
/// 字符串压缩
///
///
///
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);
}
}
///
/// 字符串解压缩
///
///
///
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 listStr)
{
if (listStr is null) return new string[] { "" };
List strList = new List();
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 strList = new List();
foreach (string s in listStr)
{
strList.Add(s.ToLower());
}
return strList.ToArray();
}
public static List setToLowerList(List listStr)
{
List strList = new List();
foreach (string s in listStr.ToArray())
{
strList.Add(s.ToLower());
}
return strList;
}
}
}