//--------------------------日期:2013-8-28 //--------------------------版本:2.0.1.0 //--------------------------作者:itrycn using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; //字符串操作类 namespace ryCommon { /// /// 字符串操作类 /// public class Strings { /// /// 字符串转Base64 /// /// /// public string StrToBase64(string input) { System.Text.Encoding encode = System.Text.Encoding.Default; byte[] bytedata = encode.GetBytes(input); return Convert.ToBase64String(bytedata, 0, bytedata.Length); } /// /// Base64转字符串 /// /// /// public string Base64ToStr(string input) { try { byte[] bpath = Convert.FromBase64String(input); return System.Text.Encoding.Default.GetString(bpath); } catch { return ""; } } /// /// 转换指定字符串为布尔类型 /// public bool StrToBool(string Str,bool defValue) { try { if (Str == "1" || Str.ToLower()=="true") { return true; } else { return false; } } catch { return defValue; } } /// /// 转换指定字符串为Double类型 /// public double StrToDouble(string Str, double defValue) { try { return Convert.ToDouble(Str); } catch { return defValue; } } /// /// 已重载.计算两个日期的时间间隔,返回的是时间间隔的日期差的绝对值. /// /// 第一个日期和时间 /// 第二个日期和时间 /// public string DateDiff(DateTime DateTime1, DateTime DateTime2) { string dateDiff = ""; try { TimeSpan ts1 = new TimeSpan(DateTime1.Ticks); TimeSpan ts2 = new TimeSpan(DateTime2.Ticks); TimeSpan ts = ts1.Subtract(ts2).Duration(); if(ts.Days!=0) { dateDiff = ts.Days.ToString() + "天"; } if (ts.Hours != 0) { dateDiff += ts.Hours.ToString() + "小时"; } if (ts.Minutes != 0) { dateDiff += ts.Minutes.ToString() + "分钟"; } if (ts.Seconds != 0) { dateDiff += ts.Seconds.ToString() + "秒"; } } catch { } if (dateDiff == "") { dateDiff = "0秒"; } return dateDiff; } /// /// 已重载.计算一个时间与当前本地日期和时间的时间间隔,返回的是时间间隔的日期差的绝对值. /// /// 一个日期和时间 /// public string DateDiff(DateTime DateTime1) { return this.DateDiff(DateTime1, DateTime.Now); } /// /// 转换指定字符串为Int类型 /// public int StrToInt(string Str, int defValue) { try { return Convert.ToInt32(Str); } catch { return defValue; } } /// /// 转换指定字符串为Int类型 /// public int StrToInt(string Str, int minValue, int maxValue, int defValue) { try { int tmpI=Convert.ToInt32(Str); if (tmpI < minValue || tmpI > maxValue) { return defValue; } else { return tmpI; } } catch { return defValue; } } /// /// 转换指定布尔类型为Int类型,true为1,false为0 /// public int BoolToInt(bool sValue) { try { if (sValue ==true) { return 1; } else { return 0; } } catch { return 0; } } /// /// 判断指定字符串是否是布尔类型 /// public bool IsBool(string sValue) { try { Convert.ToBoolean(sValue); return true; } catch { return false; } } /// /// 判断指定字符串是否是Double类型 /// public bool IsDouble(string sValue) { try { Convert.ToDouble(sValue); return true; } catch { return false; } } /// /// 判断指定字符串是否是Int类型 /// public bool IsInt(string sValue) { try { Convert.ToInt64(sValue); if (sValue.IndexOf(".")>=0) { return false; } else { return true; } } catch { return false; } } /// /// 根据年月日转换成日期 /// /// /// /// /// public DateTime ToDate(int year, int month, int day) { try { return Convert.ToDateTime(year.ToString() + "-" + month.ToString() + "-" + day.ToString()); } catch { return Convert.ToDateTime("2000-1-1"); } } /// /// 追加字符 /// public string AppendStr(string str, string addstr) { if (str == "") { return addstr; } else { if (addstr == "") { return str; } else { return str + "\r\n" + addstr; } } } /// /// 获取2个字符串中间的内容,point1为空表示从首位开始算,point2为空表示算到结尾。 /// public string GetStr(string str, string point1, string point2, int iPos1,out int endPos, string defValue) { int iIndex = 0; endPos = 0; if(iPos1>= str.Length) { return defValue; } if (point1 != "") { iIndex = str.IndexOf(point1, iPos1); } if (iIndex == -1) { return defValue; } int iIndex2 = 0; if (point2 == "") { iIndex2 = str.Length; } else { iIndex2 = str.IndexOf(point2, iIndex + point1.Length); } string tmpStr = str; if (iIndex < iIndex2 && iIndex >= 0) { tmpStr = str.Substring(iIndex + point1.Length, iIndex2 - iIndex - point1.Length); endPos = iIndex2 + point2.Length; } else { return defValue; } return tmpStr; } /// /// 判断字符串是否只包含数字或英文 /// /// /// public static bool IsEngOrNum(string str) { return !Regex.IsMatch(str, "[^0-9a-zA-Z]"); } /// /// 判断字符串是否只包含英文 /// /// /// public static bool IsEng(string str) { return !Regex.IsMatch(str, "[^a-zA-Z]"); } /// /// 判断字符串是否匹配,支持?*通配符 /// /// 待匹配的字符串 /// 匹配的通配符 /// public static bool IsMatchStr(string str, string likestr) { Regex replace = new Regex("[.$^{\\[(|)*+?\\\\]"); string a = replace.Replace(likestr, delegate (Match m) { switch (m.Value) { case "?": return ".?"; case "*": return ".*"; default: return "\\" + m.Value; } }); Regex strP = new Regex(a); return strP.IsMatch(str); //return replace.IsMatch(wildcardStr1); } /// /// 通配符替换 /// /// /// /// /// public static string ReplaceByMatch(string str, string likestr, string replacement) { Regex replace = new Regex("[.$^{\\[(|)*+?\\\\]"); string a = replace.Replace(likestr, delegate (Match m) { switch (m.Value) { case "?": return ".?"; case "*": return ".*"; default: return "\\" + m.Value; } }); Regex strP = new Regex(a); return strP.Replace(str, replacement); //return replace.IsMatch(wildcardStr1); } /// /// 支持忽略大小写的替换功能 /// /// /// /// /// 是否忽略大小写 /// public static string Replace(string str, string oldValue, string newValue,bool IgnoreCase) { if(IgnoreCase) { var str2 = ""; var str_tmp = str; var pos = str_tmp.IndexOfEx(oldValue); while(pos>=0) { str2 += str_tmp.Substring(0, pos); str_tmp = str_tmp.Substring(pos + oldValue.Length); pos = str_tmp.IndexOfEx(oldValue); } str2 += str_tmp; return str2; } else { return str.Replace(oldValue,newValue); } } /// /// 获取匹配的内容 /// /// /// /// /// public static string GetMatchStr(string str, string likestr,bool isRegex) { string _likestr = likestr; if (!isRegex) { Regex replace = new Regex("[.$^{\\[(|)*+?\\\\]"); _likestr = replace.Replace(likestr, delegate (Match m) { switch (m.Value) { case "?": return ".?"; case "*": return ".*"; default: return "\\" + m.Value; } }); } Match match = Regex.Match(str, _likestr); return match.Value; //return replace.IsMatch(wildcardStr1); } } }