using System; using System.Collections.Generic; using System.Diagnostics; using System.Text; using System.Text.RegularExpressions; //引用正则表达式 namespace ARW.Common { /// /// 常用公共类 /// public class Common { public static object _lock = new object(); public static int count = 1; #region Stopwatch计时器 /// /// 计时器开始 /// /// public static Stopwatch TimerStart() { Stopwatch watch = new Stopwatch(); watch.Reset(); watch.Start(); return watch; } /// /// 计时器结束 /// /// /// public static string TimerEnd(Stopwatch watch) { watch.Stop(); double costtime = watch.ElapsedMilliseconds; return costtime.ToString(); } #endregion #region 删除数组中的重复项 /// /// 删除数组中的重复项 /// /// /// public static string[] RemoveDup(string[] values) { List list = new List(); for (int i = 0; i < values.Length; i++)//遍历数组成员 { if (!list.Contains(values[i])) { list.Add(values[i]); }; } return list.ToArray(); } #endregion #region 自动生成编号 /// /// 表示全局唯一标识符 (GUID)。 /// /// public static string GuId() { return Guid.NewGuid().ToString(); } /// /// 自动生成编号 201008251145409865 /// /// public static string CreateNo() { Random random = new Random(); string strRandom = random.Next(1000, 10000).ToString(); //生成编号 string code = DateTime.Now.ToString("yyyyMMddHHmmss") + strRandom;//形如 return code; } /// /// 生成不重复的订单,净水器 /// /// public static string CreateNo2() { lock (_lock) { if (count >= 10000) { count = 1; } var number = DateTime.Now.ToString("yyMMddHHmmssfff") + count.ToString("0000"); count++; return number; } } /// /// 生成不重复的订单,送芯 /// /// public static string CreateNo3() { lock (_lock) { if (count >= 10000) { count = 1; } var number = "C" + DateTime.Now.ToString("yyMMddHHmmssfff") + count.ToString("0000"); count++; return number; } } /// /// 退款订单号生成 /// /// public static string CreateNo_Recharge() { lock (_lock) { if (count >= 10000) { count = 1; } var number = "R" + DateTime.Now.ToString("yyMMddHHmmssfff") + count.ToString("0000"); count++; return number; } } /// /// 生成订单号 /// /// public static string CreateNoQuery() { lock (_lock) { if (count >= 10000) { count = 1; } var number = "Q" + DateTime.Now.ToString("yyMMddHHmmssfff") + count.ToString("0000"); count++; return number; } } #endregion #region 生成0-9随机数 /// /// 生成0-9随机数 /// /// 生成长度 /// public static string RndNum(int codeNum) { StringBuilder sb = new StringBuilder(codeNum); Random rand = new Random(); for (int i = 1; i < codeNum + 1; i++) { int t = rand.Next(9); sb.AppendFormat("{0}", t); } return sb.ToString(); } #endregion #region 生成随机数 /// /// 生成随机数 /// /// 数据长度 /// 生成类型,0:纯数字,1:纯字母, 2:数据字+字母 /// public static string GetRandomNo(int length, int CreateType) { char[] charlist = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'h', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; string result = ""; Random rd = new Random(); switch (CreateType) { case 0: //纯数字 for (int i = 0; i < length; i++) { result = result + charlist[rd.Next(0, 10)]; } break; case 1://纯字母 for (int i = 0; i < length; i++) { result = result + charlist[rd.Next(10, 36)]; } break; case 2://数据字+字母 for (int i = 0; i < length; i++) { result = result + charlist[rd.Next(0, 36)]; } break; } return result; } #endregion #region 删除最后一个字符之后的字符 /// /// 删除最后结尾的一个逗号 /// public static string DelLastComma(string str) { return str.Substring(0, str.LastIndexOf(",")); } /// /// 删除最后结尾的指定字符后的字符 /// public static string DelLastChar(string str, string strchar) { return str.Substring(0, str.LastIndexOf(strchar)); } /// /// 删除最后结尾的长度 /// /// /// /// public static string DelLastLength(string str, int Length) { if (string.IsNullOrEmpty(str)) return ""; str = str.Substring(0, str.Length - Length); return str; } #endregion #region 小写转大写 /// /// 转换人民币大小金额 /// /// 金额 /// 返回大写形式 public static string CmycurD(decimal num) { string str1 = "零壹贰叁肆伍陆柒捌玖"; //0-9所对应的汉字 string str2 = "万仟佰拾亿仟佰拾万仟佰拾元角分"; //数字位所对应的汉字 string str3 = ""; //从原num值中取出的值 string str4 = ""; //数字的字符串形式 string str5 = ""; //人民币大写金额形式 int i; //循环变量 int j; //num的值乘以100的字符串长度 string ch1 = ""; //数字的汉语读法 string ch2 = ""; //数字位的汉字读法 int nzero = 0; //用来计算连续的零值是几个 int temp; //从原num值中取出的值 num = Math.Round(Math.Abs(num), 2); //将num取绝对值并四舍五入取2位小数 str4 = ((long)(num * 100)).ToString(); //将num乘100并转换成字符串形式 j = str4.Length; //找出最高位 if (j > 15) { return "溢出"; } str2 = str2.Substring(15 - j); //取出对应位数的str2的值。如:200.55,j为5所以str2=佰拾元角分 //循环取出每一位需要转换的值 for (i = 0; i < j; i++) { str3 = str4.Substring(i, 1); //取出需转换的某一位的值 temp = Convert.ToInt32(str3); //转换为数字 if (i != (j - 3) && i != (j - 7) && i != (j - 11) && i != (j - 15)) { //当所取位数不为元、万、亿、万亿上的数字时 if (str3 == "0") { ch1 = ""; ch2 = ""; nzero = nzero + 1; } else { if (str3 != "0" && nzero != 0) { ch1 = "零" + str1.Substring(temp * 1, 1); ch2 = str2.Substring(i, 1); nzero = 0; } else { ch1 = str1.Substring(temp * 1, 1); ch2 = str2.Substring(i, 1); nzero = 0; } } } else { //该位是万亿,亿,万,元位等关键位 if (str3 != "0" && nzero != 0) { ch1 = "零" + str1.Substring(temp * 1, 1); ch2 = str2.Substring(i, 1); nzero = 0; } else { if (str3 != "0" && nzero == 0) { ch1 = str1.Substring(temp * 1, 1); ch2 = str2.Substring(i, 1); nzero = 0; } else { if (str3 == "0" && nzero >= 3) { ch1 = ""; ch2 = ""; nzero = nzero + 1; } else { if (j >= 11) { ch1 = ""; nzero = nzero + 1; } else { ch1 = ""; ch2 = str2.Substring(i, 1); nzero = nzero + 1; } } } } } if (i == (j - 11) || i == (j - 3)) { //如果该位是亿位或元位,则必须写上 ch2 = str2.Substring(i, 1); } str5 = str5 + ch1 + ch2; if (i == j - 1 && str3 == "0") { //最后一位(分)为0时,加上“整” str5 = str5 + '整'; } } if (num == 0) { str5 = "零元整"; } return str5; } #endregion #region 获取html所有Img标签src地址 public static string[] GetHtmlImageUrlList(string sHtmlText) { // 定义正则表达式用来匹配 img 标签 Regex regImg = new Regex(@"]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase); // 搜索匹配的字符串 MatchCollection matches = regImg.Matches(sHtmlText); int i = 0; string[] sUrlList = new string[matches.Count]; // 取得匹配项列表 foreach (Match match in matches) sUrlList[i++] = match.Groups["imgUrl"].Value; return sUrlList; } #endregion #region 删除html标签 public static string ReplaceHtmlTag(string html, int length = 0) { string strText = System.Text.RegularExpressions.Regex.Replace(html, "<[^>]+>", ""); strText = System.Text.RegularExpressions.Regex.Replace(strText, "&[^;]+;", ""); if (length > 0 && strText.Length > length) return strText.Substring(0, length); return strText; } #endregion } }