C# WordsHelper
编码中遇到的文字相关的处理:加密、校验、金额转大写等,会不定时更新,欢迎补充:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; namespace Common.Helper { public class WordsHelper { #region 1:字符串加密 /// <summary> ///1: MD5加密 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string Md5Encryption(string str) { MD5 m5 = new MD5CryptoServiceProvider(); byte[] buffer = System.Text.Encoding.Unicode.GetBytes(str); return BitConverter.ToString(m5.ComputeHash(buffer)).Replace("-", ""); } /// <summary> /// 2: DES加密 /// </summary> /// <param name="ciphertext"></param> /// <returns></returns> private static string DESEncryption(string str) { string desPWD = ""; byte[] buffer; DESCryptoServiceProvider DesCSP = new DESCryptoServiceProvider(); MemoryStream ms = new MemoryStream();//先创建 一个内存流 CryptoStream cryStream = new CryptoStream(ms, DesCSP.CreateEncryptor(), CryptoStreamMode.Write);//将内存流连接到加密转换流 StreamWriter sw = new StreamWriter(cryStream); sw.WriteLine(str);//将要加密的字符串写入加密转换流 sw.Close(); cryStream.Close(); buffer = ms.ToArray();//将加密后的流转换为字节数组 desPWD = Convert.ToBase64String(buffer);//将加密后的字节数组转换为字符串 return desPWD; } /// <summary> /// 3:RSA加密 /// </summary> /// <param name="myKeyContainerName"></param> /// <returns></returns> private static string RSAEncryption(string str) { string ret = ""; CspParameters cp = new CspParameters(); cp.KeyContainerName = str; RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(cp); ret = rsa.ToXmlString(true); return ret; } /// <summary> /// 4:ACSII码加密 /// </summary> /// <param name="rpwd"></param> /// <returns></returns> private static string ACSIIEncryption(string str) { string Ret; byte[] array = System.Text.Encoding.ASCII.GetBytes(str); byte[] byteArray = new byte[array.Length]; for (int i = 0; i < array.Length; i++) { int asciicode = (int)(array[i]); asciicode = asciicode + 1; byteArray[i] = (byte)asciicode; } System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding(); string strCharacter = asciiEncoding.GetString(byteArray); Ret = strCharacter; return Ret; } #endregion #region 2:正则处理符串 /// <summary> /// 1:去掉字符串中的特殊字符 /// </summary> /// <param name="_path"></param> /// <returns></returns> public static string RemoveSpecialCharacter(string _path) { return Regex.Replace(_path, "[ \\[ \\] \\^ \\-_*×――(^)$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", "").ToLower(); } /// <summary> /// 2:去掉字符串末尾的数字 /// </summary> /// <param name="str"></param> /// <returns></returns> public static string RemoveEndNum(string str) { string pattern = @"(\d+)$"; Regex rgx = new Regex(pattern); string outputStr = rgx.Replace(str, ""); return outputStr; } /// <summary> /// 3:去掉字符串中的数字 /// </summary> /// <param name="key"></param> /// <returns></returns> public static string RemoveNum(string str) { return Regex.Replace(str, @"\d", ""); } /// <summary> /// 4:去掉字符串中的非数字 /// </summary> /// <param name="key"></param> /// <returns></returns> public static string RemoveNotNumber(string key) { return Regex.Replace(key, @"[^\d]*", ""); } #endregion #region 3:正则校验字符串 /// <summary> /// 1:是否手机号码 /// </summary> /// <param name="value">手机号码</param> /// <returns></returns> public static bool IsMobileNumber(string value) { if (string.IsNullOrEmpty(value)) { return false; } value = value.Trim().Replace("^", "").Replace("$", ""); /** * 手机号码: * 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[6, 7, 8], 18[0-9], 170[0-9] * 移动号段: 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705 * 联通号段: 130,131,132,155,156,185,186,145,176,1709 * 电信号段: 133,153,180,181,189,177,1700 */ return Regex.IsMatch(value, @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|70)\d{8}$"); } /// <summary> /// 2:是否邮箱 /// </summary> /// <param name="value">邮箱地址</param> /// <param name="isRestrict">是否按严格模式验证</param> /// <returns></returns> public static bool IsEmail(string value, bool isRestrict = false) { if (string.IsNullOrEmpty(value)) { return false; } string pattern = isRestrict ? @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$" : @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$"; return Regex.IsMatch(value, pattern); } /// <summary> /// 3:是否身份证号码 /// </summary> /// <param name="value">身份证</param> /// <returns></returns> public static bool IsIdCard(string value) { if (string.IsNullOrEmpty(value)) { return false; } if (value.Length == 15) { return Regex.IsMatch(value, @"^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$"); } return value.Length == 0x12 && Regex.IsMatch(value, @"^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$", RegexOptions.IgnoreCase); } #endregion /// <summary> /// 数字金额转大写 /// </summary> /// <param name="s"></param> /// <returns></returns> public string ConvertMoney(decimal s) { s = Math.Round(s, 2);//四舍五入到两位小数,即分 string[] n = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; //数字转大写 string[] d = { "", "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿" }; //不同位置的数字要加单位 List<string> needReplace = new List<string> { "零拾", "零佰", "零仟", "零万", "零亿", "亿万", "零元", "零零", "零角", "零分" }; List<string> afterReplace = new List<string> { "零", "零", "零", "万", "亿", "亿", "元", "零", "", "" };//特殊情况用replace剔除 string b = "人民币";//开头 string e = s % 1 == 0 ? "整" : "";//金额是整数要加一个“整”结尾 string re = ""; Int64 a = (Int64)(s * 100); int k = 1; while (a != 0) {//初步转换为大写+单位 re = n[a % 10] + d[k] + re; a = a / 10; k = k < 11 ? k + 1 : 4; } string need = needReplace.Where(tb => re.Contains(tb)).FirstOrDefault<string>(); while (need != null) { int i = needReplace.IndexOf(need); re = re.Replace(needReplace[i], afterReplace[i]); need = needReplace.Where(tb => re.Contains(tb)).FirstOrDefault<string>(); }//循环排除特殊情况 re = re == "" ? "" : b + re + e; return re; } } }
不积跬步,无以至千里;不积小流,无以成江海。ヾ(◍°∇°◍)ノ゙