💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## 文件路径 ``` StringHelper.cs ``` ## 1、生成唯一ID 由数字组成 ``` #region 生成唯一ID 由数字组成 /// <summary> /// 生成唯一ID /// </summary> /// <returns>返回如:1538028467155</returns> public static string CreateIDCode() { DateTime Time1 = DateTime.Now.ToUniversalTime(); DateTime Time2 = Convert.ToDateTime("1970-01-01"); TimeSpan span = Time1 - Time2; //span就是两个日期之间的差额 string t = span.TotalMilliseconds.ToString("0"); return t; } #endregion ``` ## 用法 ``` string k = StringHelper.CreateIDCode(); //返回1538028179433 ``` ## 2、用户名过滤 ``` #region 用户名过滤 /// <summary> /// 用户名过滤 /// </summary> /// <param name="userName"></param> /// <returns>返回true or false 有非法字符返回false</returns> public static bool Filter(string userName) { if (IsExist(userName,"!")) return false; if (IsExist(userName, "!")) return false; if (IsExist(userName, "#")) return false; if (IsExist(userName, "&")) return false; if (IsExist(userName, "$")) return false; if (IsExist(userName, "*")) return false; if (IsExist(userName, ".")) return false; if (IsExist(userName, ",")) return false; if (IsExist(userName, ";")) return false; if (IsExist(userName, "'")) return false; if (IsExist(userName, "<")) return false; if (IsExist(userName, ">")) return false; return true; } public static bool IsExist(string userName, string filterStr) { if (userName.IndexOf(filterStr) > -1) return true; return false; } #endregion ``` ## 用法 ``` bool k = StringHelper.Filter("dddss");//返回true bool k = StringHelper.Filter("ddd$%$%ss");//返回false ``` ## 3、字符串MD5加密 ``` #region 利用 MD5 加密算法加密字符串 /// <summary> /// 利用 MD5 加密算法加密字符串 /// </summary> /// <param name="src">字符串源串</param> /// <returns>返加MD5 加密后的字符串</returns> public static string ComputeMD5(string src) { //将密码字符串转化成字节数组 byte[] byteArray = GetByteArray(src); //计算 MD5 密码 byteArray = (new MD5CryptoServiceProvider().ComputeHash(byteArray)); //将字节码转化成字符串并返回 return BitConverter.ToString(byteArray); } /// <summary> /// 将指定串加密为不包含中杠的MD5值 /// </summary> /// <param name="str">要加密的字符串</param> /// <param name="isupper">返回值的大小写(true大写,false小写)</param> /// <returns></returns> public static string ComputeMD5(string str, bool isupper) { string md5str = ComputeMD5(str); if (isupper) return md5str.ToUpper(); return md5str.ToLower(); } #endregion ``` ## 用法 ``` string k = StringHelper.ComputeMD5("520");//CF-67-35-5A-33-33-E6-E1-43-43-91-61-AD-C2-D8-2E string k = StringHelper.ComputeMD5("520",false);//cf-67-35-5a-33-33-e6-e1-43-43-91-61-ad-c2-d8-2e string k = StringHelper.ComputeMD5("520",true);//CF-67-35-5A-33-33-E6-E1-43-43-91-61-AD-C2-D8-2E ``` ## 4、生成随机字符串 ``` #region 生成随机字符串,格式:1q2w3e4r /// <summary> /// 生成随机字符串,格式:1q2w3e4r /// </summary> /// <returns></returns> public static string BuildPassword() { Random random = new Random(); List<int> ints = new List<int>(); for (int i = 0; i < 4; i++) { ints.Add(random.Next(9)); } List<string> strs = new List<string>(); //string CodeSerial = "a,b,c,d,e,f,g,h,i,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z"; string CodeSerial = "a,b,c,d,e,f,g,h,i,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z"; string[] arr = CodeSerial.Split(','); int randValue = -1; Random rand = new Random(unchecked((int)DateTime.Now.Ticks)); for (int i = 0; i < 4; i++) { randValue = rand.Next(0, arr.Length - 1); strs.Add(arr[randValue]); } string passwd = ""; for (int k = 0; k < 4; k++) { passwd += ints[k].ToString() + strs[k]; } return passwd; } #endregion ``` ## 用法 ``` string k = StringHelper.BuildPassword();//返回5q2w1w8h ```