using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace Core.Mes.Client.Comm.Tool { /// /// 字符串处理工具 /// public class StringUtil { /// /// 校验字符串是否只包含字母与数字 /// /// 需要校验的字符串 /// true表示符合要求,false表示不符合要求 public static bool IsOnlyLetterAndDigit(string toVerified) { Regex rx = new Regex(@"^[a-zA-Z0-9-]*$"); return rx.IsMatch(toVerified.Trim(), 0); } /// /// 检验是否是整数 /// /// 需要检验的字符串 /// 是否为整数:true是整数,false非整数 public static bool IsInt(string str) { Regex rx = new Regex(@"^[0123456789]+$"); return rx.IsMatch(str); } /// /// 检验是否是整数(庹建勇) /// /// /// public static bool IsInt32(string sVal) { try { Int32.Parse(sVal); return true; } catch { return false; } } /// /// 检验是否是整数(WHB) 可以验证类似与12.0 12.00形式的字符串 /// /// 需要检验的字符串 /// 是否为整数:true是整数,false非整数 public static bool IsInteger(string str) { Regex rx = new Regex(@"^-?[1-9]\d*[.]{0,1}[0]*$"); return rx.IsMatch(str); } /// /// 校验是否为正的浮点数 /// /// 需要检验的字符串 /// 是否为正浮点,是返回true,否则返回false public static bool IsFloat(string str) { Regex rx = new Regex(@"^[0-9]*(.)?[0-9]+$", RegexOptions.IgnoreCase); return rx.IsMatch(str.Trim()); } /// /// 验证浮点数 /// /// /// public static bool IsDouble(string sVal) { try { Double.Parse(sVal); return true; } catch { return false; } } /// /// 检验是否为数字 /// /// 需要检验的字符串 /// 是否为数字:true代表是,false代表否 public static bool IsNumber(string str) { Regex rx = new Regex(@"^[+-]?[0123456789]*[.]?[0123456789]*$"); return rx.IsMatch(str); } /// /// 检验字符串是否为日期时间 /// /// 需要检验的字符串 /// 是否为日期时间:true代表是,false代表否 public static bool IsNotDateTime(string str) { DateTime dt = new DateTime(); return (!(DateTime.TryParse(str, out dt))); } /// /// 检验字符串是否为邮政编码 /// /// 需要检验的字符串 /// 是否为邮政编码:true代表是,false代表否 public static bool IsPostCode(string str) { Regex rx = new Regex(@"^[0123456789]{6}$"); return rx.IsMatch(str); } /// /// 检验字符串是否为身份证号 /// /// 需要检验的字符串 /// 是否为身份证号:true代表是,false代表否 public static bool IsCode(string str) { Regex rx = new Regex(@"^[0123456789]{15,18}$"); return rx.IsMatch(str); } /// /// 检验字符串是否为电子邮件 /// /// 需要检验的字符串 /// 是否为电子邮件:true代表是,false代表否 public static bool IsEMail(string str) { Regex rx = new Regex(@"w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*"); return rx.IsMatch(str); } /// /// 检验字符串是否为中国地区的电话号码 /// /// 需要检验的字符串 /// 是否为中国地区的电话号码:true代表是,false代表否 public static bool IsPhoneNumber(string str) { Regex rx = new Regex(@"((d{3,4})|d{3,4}-)?d{7,8}(-d{3})*"); return rx.IsMatch(str); } /// /// 检验字符串是否为汉字 /// /// 需要检验的字符串 /// 是否为汉字:true代表是,false代表否 public static bool IsChinese(string str) { Regex rx = new Regex(@"u4e00-u9fa5"); return rx.IsMatch(str); } /// /// 检验字符串是否为双字节字符(包括汉字) /// /// 需要检验的字符串 /// 是否为双字节字符:true代表是,false代表否 public static bool IsDoubleByteChar(string str) { Regex rx = new Regex(@"[^x00-xff]"); return rx.IsMatch(str); } /// /// 检验字符串是否为URL地址 /// /// 需要检验的字符串 /// 是否为URL地址:true代表是,false代表否 public static bool IsURLAddress(string str) { Regex rx = new Regex(@"[a-zA-z]+://[^s]*"); return rx.IsMatch(str); } /// /// 检验字符串是否为IP地址 /// /// 需要检验的字符串 /// 是否为IP地址:true代表是,false代表否 public static bool IsIPAddress(string str) { Regex rx = new Regex(@"d+.d+.d+.d+"); return rx.IsMatch(str); } /// /// 清除字符串中的HTML标签(对于复杂的嵌套标签有时不准确) /// /// 指定的要被处理的字符串 /// 清除HTML标签后的字符串 public static string RemoveHtmlTags(string toEvaluate) { Regex rx = new Regex(@"s/<[a-zA-Z/][^>]*>//g", RegexOptions.IgnoreCase); return rx.Replace(toEvaluate, ""); } /// /// 判断输入的字符串是否完全匹配正则 /// /// 正则表达式 /// 待判断的字符串 /// public static bool IsValiable(string RegexExpression, string str) { bool blResult = false; Regex rep = new Regex(RegexExpression, RegexOptions.IgnoreCase); //blResult = rep.IsMatch(str); Match mc = rep.Match(str); if (mc.Success) { if (mc.Value == str) blResult = true; } return blResult; } /// /// 字符串转是否能转换成非负数 /// /// /// public static bool Is_Below_zero(string str) { try { if (IsDouble(str) && double.Parse(str) > 0) return true; else return false; } catch { return false; } } /// /// 全单位修约(GB 8170-87) /// /// 原始值 /// 10的n次方修约间隔 /// 修约后值 public static string XYFullUnit(string sOriVal, sbyte ten_interval) { try { decimal interval = Convert.ToDecimal(Math.Pow(10, ten_interval));//修约间隔 decimal d = decimal.Parse(sOriVal) / interval; bool isPlus = (d > 0); //是否为正数 d = Math.Abs(d); int i = (int)d; if ((d - i) < 0.5m) { d = i * interval; } else if ((d - i) > 0.5m) { d = (i + 1) * interval; } else //(d-i) == 0.5m { if ((i % 2) == 0) //双数舍去 d = i * interval; else //单数进一 d = (i + 1) * interval; } if (!isPlus) d = 0 - d; if (ten_interval >= 0) return d.ToString(); else return ValWithDigits(d.ToString(), Math.Abs(ten_interval)); } catch { return ""; } } /// /// 保留指定位数小数 /// /// /// /// public static string ValWithDigits(string sVal, int Digits) { try { decimal d = decimal.Parse(sVal); string sFormat = "0"; if (Digits > 0) { sFormat += "."; for (int i = 0; i < Digits; i++) sFormat += "0"; } return d.ToString(sFormat); } catch { return ""; } } /// /// 值比对 sSign:符号,sMin:最小值,sMax:最大值,sRealVal:实际值 /// /// /// /// /// /// public static bool ValIsEligible(string sSign, string sMin, string sMax, string sRealVal) { try { switch (sSign) { case ">": return (double.Parse(sMin) < double.Parse(sRealVal)); case ">=": return (double.Parse(sMin) <= double.Parse(sRealVal)); case "=": if (IsDouble(sMin)) return (double.Parse(sMin) == double.Parse(sRealVal)); else return (sMin == sRealVal); case "<": return (double.Parse(sMax) > double.Parse(sRealVal)); case "<=": return (double.Parse(sMax) >= double.Parse(sRealVal)); default: return true; } } catch { return false; } } /// /// 数字转换成中文数字 /// /// /// public static string ConvertNumberToChinese(string strNum) { string[] Nums = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; string[] Digits = { "", "拾", "佰", "仟" }; string[] Units = { "元", "万", "亿", "万亿" }; string x, y, z = ""; if (strNum.Length > 2) { x = strNum.Substring(0, strNum.Length - 2); y = strNum.Substring(strNum.Length - 2, 2); } else { x = ""; y = strNum; } if (y.Length == 2) { int n = Convert.ToInt32(y.Substring(0, 1)); z = Nums[n] + "角"; } if (y.Length > 0) { int n = Convert.ToInt32(y.Substring(y.Length - 1, 1)); z += Nums[n] + "分"; } if (y.Length == 0) { if (x.Length == 0) z = "零元整"; else z = "整"; } string S = ""; //返回值 int p = 0; //字符位置指针 int m = x.Length % 4; //取模 // 四位一组得到组数 int k = (m > 0 ? x.Length / 4 + 1 : x.Length / 4); // 外层循环在所有组中循环 // 从左到右 高位到低位 四位一组 逐组处理 // 每组最后加上一个单位: "[万亿]","[亿]","[万]" for (int i = k; i > 0; i--) { int L = 4; if (i == k && m != 0) { L = m; } // 得到一组四位数 最高位组有可能不足四位 string s = x.Substring(p, L); int l = s.Length; // 内层循环在该组中的每一位数上循环 从左到右 高位到低位 for (int j = 0; j < l; j++) { //处理改组中的每一位数加上所在位: "仟","佰","拾",""(个) int n = Convert.ToInt32(s.Substring(j, 1)); if (n == 0) { if (j < l - 1 && Convert.ToInt32(s.Substring(j + 1, 1)) > 0 //后一位(右低) && !S.EndsWith(Nums[n])) { S += Nums[n]; } } else { //处理 1013 一千零"十三", 1113 一千一百"一十三" if (!(n == 1 && (S.EndsWith(Nums[0]) | S.Length == 0) && j == l - 2)) { S += Nums[n]; } S += Digits[l - j - 1]; } } p += L; // 每组最后加上一个单位: [万],[亿] 等 if (i < k) //不是最高位的一组 { if (Convert.ToInt32(s) != 0) { //如果所有 4 位不全是 0 则加上单位 [万],[亿] 等 S += Units[i - 1]; } } else { //处理最高位的一组,最后必须加上单位 S += Units[i - 1]; } } return S + z; } /// /// 序号自增长 /// /// 字母加数字的序列号,如"A001","SS0002","003" /// String 原序号字符串中数字加1,如"A002","SS0003","004" public static String SequenceIncrease(String sequence) { String newSequence = ""; if (!String.IsNullOrEmpty(sequence)) { int index = sequence.IndexOfAny("0123456789".ToCharArray()); try { String strFormat = Math.Pow(10, sequence.Length - index).ToString().Substring(1); int num = (Convert.ToInt32(sequence.Substring(index)) + 1); if (num < Math.Pow(10, strFormat.Length)) { newSequence = sequence.Substring(0, index) + num.ToString(strFormat); } else { throw (new MESException("序列号已超出最大值")); } } catch (Exception ex) { throw (new MESException("序列号格式不正确,必须以字母加数字格式,如\"A001\",\"SS0002\",\"003\"")); } } return newSequence; } } }