using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ryCommon { /// /// 对变量的拓展 /// public static class VarExtension { /// /// 替换文本(忽略大小写) /// /// /// 需要替换的内容 /// 替换后的内容 /// static public string ReplaceEx(this string original, string pattern, string replacement) { int count, position0, position1; count = position0 = position1 = 0; string upperString = original.ToUpper(); string upperPattern = pattern.ToUpper(); int inc = (original.Length / pattern.Length) * (replacement.Length - pattern.Length); char[] chars = new char[original.Length + Math.Max(0, inc)]; while ((position1 = upperString.IndexOf(upperPattern, position0)) != -1) { for (int i = position0; i < position1; ++i) chars[count++] = original[i]; for (int i = 0; i < replacement.Length; ++i) chars[count++] = replacement[i]; position0 = position1 + pattern.Length; } if (position0 == 0) return original; for (int i = position0; i < original.Length; ++i) chars[count++] = original[i]; return new string(chars, 0, count); } /// /// 替换文本(忽略大小写),可支持通配符 /// /// /// /// /// 是否是通配符表达式,不是,则普通替换,忽略大小写,是,则启用通配符替换 /// static public string ReplaceEx(this string original, string pattern, string replacement,bool isLike) { if(isLike) { return Strings.ReplaceByMatch(original, pattern, replacement); } else { return ReplaceEx(original, pattern, replacement); } } /// /// 支持忽略大小写的替换文本功能 /// /// /// /// /// 是否忽略大小写 /// static public string Replace(this string original, string oldValue, string newValue, bool IgnoreCase) { return Strings.Replace(original, oldValue, newValue, IgnoreCase); } /// /// 获取符合要求的第一个结果 /// /// /// /// 是否是正则 /// static public string GetMatchStr(this string input, string value, bool isRegex) { return Strings.GetMatchStr(input,value,isRegex); } /// /// 查找字符串,忽略大小写 /// /// /// /// /// static public int IndexOfEx(this string input, string value, int startindex) { return input.IndexOf(value,startindex,StringComparison.OrdinalIgnoreCase); } /// /// 查找字符串,忽略大小写 /// /// /// /// static public int IndexOfEx(this string input, string value) { return input.IndexOf(value, StringComparison.OrdinalIgnoreCase); } /// /// 判断字符串是否是数字或英文 /// /// /// static public bool IsEngOrNum(this string input) { return Strings.IsEngOrNum(input); } /// /// 判断字符串是否是英文 /// /// /// static public bool IsEng(this string input) { return Strings.IsEng(input); } /// /// 判断字符串是否是数字(不同于IsInt,本函数是判断字符串是否只含有数字,对字符串长度没有限制) /// /// /// static public bool IsNum(this string input) { return Strings.IsNum(input); } /// /// 是否在指定范围内 /// /// /// /// /// static public bool IsInRange(this decimal input, decimal min, decimal max) { return (input >= min && input <= max); } /// /// 是否在指定范围内 /// /// /// /// /// static public bool IsInRange(this int input, int min, int max) { return (input >= min && input <= max); } /// /// 转换指定字符串为Int类型 /// /// /// 最小值 /// 最大值 /// 如果字符串不在范围内,则使用本默认值 /// static public int ToInt(this object input, int minValue, int maxValue, int defValue) { try { int tmpI = Convert.ToInt32(input); if (tmpI < minValue || tmpI > maxValue) { return defValue; } else { return tmpI; } } catch { return defValue; } } /// /// 转换指定布尔类型到Int类型,true为1,false为0 /// /// /// static public int ToInt(this bool input) { return input?1:0; } /// /// 将指定类型转换成整型 /// /// /// /// static public int ToInt(this object input, int defValue) { try { return Convert.ToInt32(input); } catch { return defValue; } } /// /// 将数字转换成字符串,小数点末尾后面不包含0 /// /// /// static public string ToNString(this decimal input) { var str= input.ToString(); if (str.IndexOfEx(".") >= 0) { str= str.TrimEnd('0'); if(str.EndsWith(".")) { str = str.Substring(0, str.Length - 1); } } return str; } /// /// 将数字转换成字符串,小数点末尾后面不包含0 /// /// /// static public string ToNString(this double input) { var str = input.ToString(); if (str.IndexOfEx(".") >= 0) { str = str.TrimEnd('0'); if (str.EndsWith(".")) { str = str.Substring(0, str.Length - 1); } } return str; } /// /// 将指定类型转换成整型 /// /// /// static public int ToInt(this object input) { try { if (input == null) { return 0; } if (input is string) { if(input.IsDouble()) { return Convert.ToInt32(Math.Round(Convert.ToDouble(input))); } } return Convert.ToInt32(input); } catch(Exception) { return 0; } } /// /// 将指定类型转换成长整型,如果时间时间类型,则转换为Unix时间戳 /// /// /// /// static public long ToInt64(this object input, Int64 defValue) { try { if (input == null) { return defValue; } if (input is DateTime time) { return time.ToTimeStamp(); } return Convert.ToInt64(input); } catch { return defValue; } } /// /// 转换为Unix时间戳 /// /// /// static public long ToInt64(this DateTime input) { return input.ToTimeStamp(); } /// /// 转换为Js时间戳 /// /// /// static public long ToJsTime(this DateTime input) { return RyDate.DateTimeToJSTime(input); } /// /// 转换为Unix时间戳 /// /// /// static public long ToUnixTime(this DateTime input) { return RyDate.DateTimeToUnixTime(input); } /// /// 将指定类型转换成长整型 /// /// /// static public long ToInt64(this object input) { return ToInt64(input,0); } /// /// 转换指定类型为Double类型 /// /// /// /// static public double ToDouble(this object input, double defValue) { try { return Convert.ToDouble(input); } catch { return defValue; } } /// /// 转换指定类型为Double类型 /// /// /// static public double ToDouble(this object input) { try { return Convert.ToDouble(input); } catch { return 0d; } } /// /// 转换DateTime类型到日期时间字符串 /// /// /// static public string ToDateTimeStr(this DateTime input) { return input.ToString("yyyy-MM-dd HH:mm:ss"); } /// /// 转换DateTime类型到日期字符串 /// /// /// static public string ToDateStr(this DateTime input) { return input.ToString("yyyy-MM-dd"); } /// /// 转换DateTime类型到日期星期字符串 /// /// /// static public string ToDateWeekStr(this DateTime input) { return input.ToString("yyyy-MM-dd dddd"); } /// /// 转换DateTime类型到Unix时间戳 /// /// /// static public long ToTimeStamp(this DateTime input) { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区 long timeStamp = (long)(input - startTime).TotalSeconds; // 相差秒数 return timeStamp; } /// /// 转换Unix时间戳到DateTime类型 /// /// /// static public DateTime ToDateTime(this long input) { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1)); // 当地时区 return startTime.AddSeconds(input); } /// /// 判断字符串是否与内容匹配 /// /// /// /// static public bool IsMatchStr(this string input, string likestr) { return Strings.IsMatchStr(input,likestr); } /// /// 将变量值转换成Double类型,如果不在指定区域范围内,则使用默认值 /// /// /// /// /// /// static public double ToDouble(this object input, double minValue, double maxValue, double defValue) { try { double tmpI = Convert.ToDouble(input); if (tmpI < minValue || tmpI > maxValue) { return defValue; } else { return tmpI; } } catch { return defValue; } } /// /// 转换到文件大小字符串 /// /// /// static public string ToFileSizeStr(this Int64 input) { return RyFiles.GetFileSizeStr(input); } /// /// 转换到文件大小字符串 /// /// /// static public string ToFileSizeStr(this UInt64 input) { return RyFiles.GetFileSizeStr(input.ToInt64(0)); } /// /// 转换指定类型为Decimal类型 /// /// /// /// static public decimal ToDecimal(this object input, decimal defValue) { try { return Convert.ToDecimal(input); } catch { return defValue; } } /// /// 将变量值转换成Decimal类型 /// /// /// static public decimal ToDecimal(this object input) { try { return Convert.ToDecimal(input); } catch { return 0; } } /// /// 将变量值转换成Decimal类型,如果不在指定区域范围内,则使用默认值 /// /// /// /// /// /// static public decimal ToDecimal(this object input, decimal minValue, decimal maxValue, decimal defValue) { try { decimal tmpI = Convert.ToDecimal(input); if (tmpI < minValue || tmpI > maxValue) { return defValue; } else { return tmpI; } } catch { return defValue; } } /// /// 转换指定类型为DateTime类型 /// /// /// /// static public DateTime ToDateTime(this object input, DateTime defValue) { try { return Convert.ToDateTime(input); } catch { return defValue; } } /// /// 将变量值转换成DateTime类型 /// /// /// static public DateTime ToDateTime(this object input) { try { return Convert.ToDateTime(input); } catch { return Convert.ToDateTime("2000-1-1"); } } /// /// 转换指定类型为布尔类型 /// /// /// static public bool ToBool(this object input) { return (input.ToString() == "1" || input.ToString().ToLower() == "true" || input is true); } /// /// 追加字符 /// /// /// /// static public string AppendStr(this string input,string addstr) { if (input == "") { return addstr; } else { if (addstr == "") { return input; } else { return input + "\r\n" + addstr; } } } /// /// 获取指定字符串之间的内容 /// /// /// /// /// static public string GetStr(this string input, string point1, string point2) { return GetStr(input, point1, point2, 0,out _, ""); } /// /// 获取指定字符串之间的内容 /// /// /// /// /// /// /// /// static public string GetStr(this string input, string point1, string point2, int iPos1, out int endPos, string defValue) { endPos = -1; if (input == null) { return defValue; } int iIndex = iPos1; if (iPos1 < 0) { return defValue; } if (iPos1>input.Length-1) { return defValue; } if (point1 != "") { iIndex = input.IndexOf(point1, iPos1); } if (iIndex == -1) { return defValue; } int iIndex2; if (point2 == "") { iIndex2 = input.Length; } else { iIndex2 = input.IndexOf(point2, iIndex + point1.Length); } string tmpStr; if (iIndex < iIndex2 && iIndex >= 0) { tmpStr = input.Substring(iIndex + point1.Length, iIndex2 - iIndex - point1.Length); endPos = iIndex2 + point2.Length; } else { return defValue; } return tmpStr; } /// /// 判断指定字符串是否是Int类型 /// /// /// static public bool IsInt(this object input) { try { Convert.ToInt64(input); return input.ToString().IndexOf(".") < 0; } catch { return false; } } /// /// 判断指定字符串是否是Double类型 /// /// /// static public bool IsDouble(this object input) { try { Convert.ToDouble(input); return true; } catch { return false; } } /// /// 判断是否是布尔类型 /// /// /// static public bool IsBool(this object input) { try { Convert.ToBoolean(input); return true; } catch { return false; } } /// /// 将字符串转换为Base64类型 /// /// /// static public string ToBase64(this string input) { System.Text.Encoding encode = System.Text.Encoding.Default; byte[] bytedata = encode.GetBytes(input); return Convert.ToBase64String(bytedata, 0, bytedata.Length); } /// /// 判断字符串是否在指定长度 /// /// /// /// /// static public bool InLength(this string input,int min,int max) { if (input.Length >= min && input.Length <= max) { return true; } else return false; } /// /// 判断能否转换成日期格式 /// /// /// static public bool IsDateTime(this object input) { try { Convert.ToDateTime(input); return true; } catch { return false; } } /// /// 根据表单字段名,获取对应的值 /// /// /// /// /// static public string Get(this Tuple>> input,string Name,string defValue) { try { return RyWeb.WebDecode.GetParam(input, Name, defValue); } catch { return defValue; } } /// /// 根据表单字段名,获取对应的值 /// /// /// /// static public string Get(this Tuple>> input, string Name) { return input.Get(Name, ""); } /// /// 根据表单字段名,获取对应的值 /// /// /// /// /// static public int Get(this Tuple>> input, string Name,int defValue) { return input.Get(Name, defValue.ToString()).ToInt(); } /// /// 根据表单字段名,获取对应的值 /// /// /// /// /// static public long Get(this Tuple>> input, string Name, long defValue) { return input.Get(Name, defValue.ToString()).ToInt64(); } /// /// 根据表单字段名,获取对应的值 /// /// /// /// /// static public decimal Get(this Tuple>> input, string Name, decimal defValue) { return input.Get(Name, defValue.ToString()).ToDecimal(); } /// /// 根据表单字段名,获取对应的值 /// /// /// /// /// static public double Get(this Tuple>> input, string Name, double defValue) { return input.Get(Name, defValue.ToString()).ToDouble(); } /// /// 根据表单字段名,获取对应的值 /// /// /// /// /// static public bool Get(this Tuple>> input, string Name, bool defValue) { return input.Get(Name, defValue.ToString()).ToBool(); } } }