c# 工具类(字符串和时间,文件)

  1. using System;  
  2. using System.IO;  
  3. using System.Text.RegularExpressions;  
  4. using System.Windows.Browser;  
  5.   
  6. namespace SL_COMMON  
  7. {  
  8.     public class Utils  
  9.     {  
  10.         #region String字符串类  
  11.   
  12.         /**/  
  13.         /// <summary>  
  14.         /// 过滤字符  
  15.         /// </summary>  
  16.   
  17.         public static string Replace(string strOriginal, string oldchar, string newchar)  
  18.         {  
  19.             if (string.IsNullOrEmpty(strOriginal))  
  20.                 return "";  
  21.             string tempChar = strOriginal;  
  22.             tempChar = tempChar.Replace(oldchar, newchar);  
  23.   
  24.             return tempChar;  
  25.         }  
  26.   
  27.         /**/  
  28.         /// <summary>  
  29.         /// 过滤非法字符  
  30.         /// </summary>  
  31.         /// <param name="str"></param>  
  32.         /// <returns></returns>  
  33.         public static string ReplaceBadChar(string str)  
  34.         {  
  35.             if (string.IsNullOrEmpty(str))  
  36.                 return "";  
  37.             string strBadChar, tempChar;  
  38.             string[] arrBadChar;  
  39.             strBadChar = "@@,+,',--,%,^,&,?,(,),<,>,[,],{,},/,\\,;,:,\",\"\",";  
  40.             arrBadChar = SplitString(strBadChar, ",");  
  41.             tempChar = str;  
  42.             for (int i = 0; i < arrBadChar.Length; i++)  
  43.             {  
  44.                 if (arrBadChar[i].Length > 0)  
  45.                     tempChar = tempChar.Replace(arrBadChar[i], "");  
  46.             }  
  47.             return tempChar;  
  48.         }  
  49.   
  50.   
  51.         /**/  
  52.         /// <summary>  
  53.         /// 检查是否含有非法字符  
  54.         /// </summary>  
  55.         /// <param name="str">要检查的字符串</param>  
  56.         /// <returns></returns>  
  57.         public static bool ChkBadChar(string str)  
  58.         {  
  59.             bool result = false;  
  60.             if (string.IsNullOrEmpty(str))  
  61.                 return result;  
  62.             string strBadChar, tempChar;  
  63.             string[] arrBadChar;  
  64.             strBadChar = "@@,+,',--,%,^,&,?,(,),<,>,[,],{,},/,\\,;,:,\",\"\"";  
  65.             arrBadChar = SplitString(strBadChar, ",");  
  66.             tempChar = str;  
  67.             for (int i = 0; i < arrBadChar.Length; i++)  
  68.             {  
  69.                 if (tempChar.IndexOf(arrBadChar[i]) >= 0)  
  70.                     result = true;  
  71.             }  
  72.             return result;  
  73.         }  
  74.   
  75.   
  76.         /**/  
  77.         /// <summary>  
  78.         /// 分割字符串  
  79.         /// </summary>  
  80.         public static string[] SplitString(string strContent, string strSplit)  
  81.         {  
  82.             if (string.IsNullOrEmpty(strContent))  
  83.             {  
  84.                 return null;  
  85.             }  
  86.             int i = strContent.IndexOf(strSplit);  
  87.             if (strContent.IndexOf(strSplit) < 0)  
  88.             {  
  89.                 string[] tmp = { strContent };  
  90.                 return tmp;  
  91.             }  
  92.             //return Regex.Split(strContent, @strSplit.Replace(".", @"\."), RegexOptions.IgnoreCase);  
  93.   
  94.             return Regex.Split(strContent, @strSplit.Replace(".", @"\."));  
  95.         }  
  96.   
  97.   
  98.         /**/  
  99.         /// <summary>  
  100.         /// string型转换为int型  
  101.         /// </summary>  
  102.         /// <param name="strValue">要转换的字符串</param>  
  103.         /// <returns>转换后的int类型结果.如果要转换的字符串是非数字,则返回-1.</returns>  
  104.         public static int StrToInt(object strValue)  
  105.         {  
  106.             int defValue = -1;  
  107.             if ((strValue == null) || (strValue.ToString() == string.Empty) || (strValue.ToString().Length > 10))  
  108.             {  
  109.                 return defValue;  
  110.             }  
  111.   
  112.             string val = strValue.ToString();  
  113.             string firstletter = val[0].ToString();  
  114.   
  115.             if (val.Length == 10 && IsNumber(firstletter) && int.Parse(firstletter) > 1)  
  116.             {  
  117.                 return defValue;  
  118.             }  
  119.             else if (val.Length == 10 && !IsNumber(firstletter))  
  120.             {  
  121.                 return defValue;  
  122.             }  
  123.   
  124.   
  125.             int intValue = defValue;  
  126.             if (strValue != null)  
  127.             {  
  128.                 bool IsInt = new Regex(@"^([-]|[0-9])[0-9]*$").IsMatch(strValue.ToString());  
  129.                 if (IsInt)  
  130.                 {  
  131.                     intValue = Convert.ToInt32(strValue);  
  132.                 }  
  133.             }  
  134.   
  135.             return intValue;  
  136.         }  
  137.   
  138.         /**/  
  139.         /// <summary>  
  140.         /// string型转换为int型  
  141.         /// </summary>  
  142.         /// <param name="strValue">要转换的字符串</param>  
  143.         /// <param name="defValue">缺省值</param>  
  144.         /// <returns>转换后的int类型结果</returns>  
  145.         public static int StrToInt(object strValue, int defValue)  
  146.         {  
  147.             if ((strValue == null) || (strValue.ToString() == string.Empty) || (strValue.ToString().Length > 10))  
  148.             {  
  149.                 return defValue;  
  150.             }  
  151.   
  152.             string val = strValue.ToString();  
  153.             string firstletter = val[0].ToString();  
  154.   
  155.             if (val.Length == 10 && IsNumber(firstletter) && int.Parse(firstletter) > 1)  
  156.             {  
  157.                 return defValue;  
  158.             }  
  159.             else if (val.Length == 10 && !IsNumber(firstletter))  
  160.             {  
  161.                 return defValue;  
  162.             }  
  163.   
  164.   
  165.             int intValue = defValue;  
  166.             if (strValue != null)  
  167.             {  
  168.                 bool IsInt = new Regex(@"^([-]|[0-9])[0-9]*$").IsMatch(strValue.ToString());  
  169.                 if (IsInt)  
  170.                 {  
  171.                     intValue = Convert.ToInt32(strValue);  
  172.                 }  
  173.             }  
  174.   
  175.             return intValue;  
  176.         }  
  177.   
  178.    
  179.   
  180.         /**/  
  181.         /// <summary>  
  182.         /// string型转换为时间型  
  183.         /// </summary>  
  184.         /// <param name="strValue">要转换的字符串</param>  
  185.         /// <param name="defValue">缺省值</param>  
  186.         /// <returns>转换后的时间类型结果</returns>  
  187.         public static DateTime StrToDateTime(object strValue, DateTime defValue)  
  188.         {  
  189.             if ((strValue == null) || (strValue.ToString().Length > 20))  
  190.             {  
  191.                 return defValue;  
  192.             }  
  193.   
  194.             DateTime intValue;  
  195.   
  196.             if (!DateTime.TryParse(strValue.ToString(), out intValue))  
  197.             {  
  198.                 intValue = defValue;  
  199.             }  
  200.             return intValue;  
  201.         }  
  202.   
  203.   
  204.         /**/  
  205.         /// <summary>  
  206.         /// 判断给定的字符串(strNumber)是否是数值型  
  207.         /// </summary>  
  208.         /// <param name="strNumber">要确认的字符串</param>  
  209.         /// <returns>是则返加true 不是则返回 false</returns>  
  210.         public static bool IsNumber(string strNumber)  
  211.         {  
  212.             return new Regex(@"^([0-9])[0-9]*(\.\w*)?$").IsMatch(strNumber);  
  213.         }  
  214.   
  215.   
  216.         /**/  
  217.         /// <summary>  
  218.         /// 检测是否符合email格式  
  219.         /// </summary>  
  220.         /// <param name="strEmail">要判断的email字符串</param>  
  221.         /// <returns>判断结果</returns>  
  222.         public static bool IsValidEmail(string strEmail)  
  223.         {  
  224.             return Regex.IsMatch(strEmail, @"^([\w-\.]+)@((
    [09]1,3\.[09]1,3\.[09]1,3\.)|(([\w]+\.)+))([azAZ]2,4|[09]1,3)(
    ?)$");  
  225.         }  
  226.   
  227.   
  228.         /**/  
  229.         /// <summary>  
  230.         /// 检测是否符合url格式,前面必需含有http://  
  231.         /// </summary>  
  232.         /// <param name="url"></param>  
  233.         /// <returns></returns>  
  234.         public static bool IsURL(string url)  
  235.         {  
  236.             return Regex.IsMatch(url, @"^http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?$");  
  237.         }  
  238.   
  239.         /**/  
  240.         /// <summary>  
  241.         /// 检测是否符合电话格式  
  242.         /// </summary>  
  243.         /// <param name="phoneNumber"></param>  
  244.         /// <returns></returns>  
  245.         public static bool IsPhoneNumber(string phoneNumber)  
  246.         {  
  247.             return Regex.IsMatch(phoneNumber, @"^(\d3|\d{3}-)?\d{7,8}$");  
  248.         }  
  249.   
  250.    
  251.   
  252.         /**/  
  253.         /// <summary>  
  254.         /// 检测是否符合身份证号码格式  
  255.         /// </summary>  
  256.         /// <param name="num"></param>  
  257.         /// <returns></returns>  
  258.         public static bool IsIdentityNumber(string num)  
  259.         {  
  260.             return Regex.IsMatch(num, @"^\d{17}[\d|X]|\d{15}$");  
  261.         }  
  262.  
  263.   
  264.  
  265.  
  266.         #endregion  
  267.  
  268.         #region Sql类  
  269.   
  270.         /**/  
  271.         /// <summary>  
  272.         /// 检测是否有Sql危险字符  
  273.         /// </summary>  
  274.         /// <param name="str">要判断字符串</param>  
  275.         /// <returns>判断结果</returns>  
  276.         public static bool IsSafeSqlString(string str)  
  277.         {  
  278.   
  279.             return !Regex.IsMatch(str, @"[-|;|,|\/|||
    |
    |\}|\{|%|@|\*|!|\']");  
  280.         }  
  281.   
  282.   
  283.         /**/  
  284.         /// <summary>  
  285.         /// 替换sql语句中的单引号  
  286.         /// </summary>  
  287.         public static string ReplaceBadSQL(string str)  
  288.         {  
  289.             string str2;  
  290.   
  291.             if (str == null)  
  292.             {  
  293.                 str2 = "";  
  294.             }  
  295.             else  
  296.             {  
  297.                 str = str.Replace("'", "''");  
  298.                 str2 = str;  
  299.             }  
  300.             return str2;  
  301.         }  
  302.  
  303.   
  304.  
  305.         #endregion  
  306.  
  307.         #region Html类  
  308.   
  309.         /**/  
  310.         /// <summary>  
  311.         /// 返回 HTML 字符串的解码结果  
  312.         /// </summary>  
  313.         /// <param name="str">字符串</param>  
  314.         /// <returns>解码结果</returns>  
  315.         public static string HtmlDecode(string str)  
  316.         {  
  317.             //str = str.Replace("''", "'");  
  318.             return HttpUtility.HtmlDecode(str);  
  319.         }  
  320.   
  321.         /**/  
  322.         /// <summary>  
  323.         /// 替换html字符  
  324.         /// </summary>  
  325.         public static string EncodeHtml(string strHtml)  
  326.         {  
  327.             if (strHtml != "")  
  328.             {  
  329.                 strHtml = strHtml.Replace(",", "&def");  
  330.                 strHtml = strHtml.Replace("'", "&dot");  
  331.                 strHtml = strHtml.Replace(";", "&dec");  
  332.                 return strHtml;  
  333.             }  
  334.             return "";  
  335.         }  
  336.   
  337.         /**/  
  338.         /// <summary>  
  339.         /// 替换回车换行符为html换行符  
  340.         /// </summary>  
  341.         public static string StrFormat(string str)  
  342.         {  
  343.             string str2;  
  344.   
  345.             if (str == null)  
  346.             {  
  347.                 str2 = "";  
  348.             }  
  349.             else  
  350.             {  
  351.                 str = str.Replace("\r\n", "<br />");  
  352.                 str = str.Replace("\n", "<br />");  
  353.                 str2 = str;  
  354.             }  
  355.             return str2;  
  356.         }  
  357.         #endregion  
  358.  
  359.         #region DateTime类  
  360.         /**/  
  361.         /// <summary>  
  362.         /// 返回当前服务器时间的 yyyy-MM-dd 日期格式string    
  363.         /// </summary>  
  364.         public static string GetDate()  
  365.         {  
  366.             return DateTime.Now.ToString("yyyy-MM-dd");  
  367.         }  
  368.   
  369.         /**/  
  370.         /// <summary>  
  371.         ///返回当前服务器时间的标准时间格式string HH:mm:ss  
  372.         /// </summary>  
  373.         public static string GetTime()  
  374.         {  
  375.             return DateTime.Now.ToString("HH:mm:ss");  
  376.         }  
  377.         /**/  
  378.         /// <summary>  
  379.         /// 返回当前服务器时间的标准时间格式string yyyy-MM-dd HH:mm:ss  
  380.         /// </summary>  
  381.         public static string GetDateTime()  
  382.         {  
  383.             return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");  
  384.         }  
  385.   
  386.         /**/  
  387.         /// <summary>  
  388.         /// 返回当前服务器时间的标准时间格式string yyyy-MM-dd HH:mm:ss:fffffff  
  389.         /// </summary>  
  390.         public static string GetDateTimeF()  
  391.         {  
  392.             return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fffffff");  
  393.         }  
  394.   
  395.         /**/  
  396.         /// <summary>  
  397.         /// 将string类型的fDateTime转换为formatStr格式的日期类型  
  398.         /// </summary>        
  399.         public static string GetStandardDateTime(string fDateTime, string formatStr)  
  400.         {  
  401.             DateTime s = Convert.ToDateTime(fDateTime);  
  402.             return s.ToString(formatStr);  
  403.         }  
  404.   
  405.         /**/  
  406.         /// <summary>  
  407.         ///将string类型的fDateTime转换为日期类型 yyyy-MM-dd HH:mm:ss  
  408.         /// </sumary>  
  409.         public static string GetStandardDateTime(string fDateTime)  
  410.         {  
  411.             return GetStandardDateTime(fDateTime, "yyyy-MM-dd HH:mm:ss");  
  412.         }  
  413.         /**/  
  414.         /// <summary>  
  415.         /// 返回相差的秒数  
  416.         /// </summary>  
  417.         /// <param name="Time"></param>  
  418.         /// <param name="Sec"></param>  
  419.         /// <returns></returns>  
  420.         public static int StrDateDiffSeconds(string Time, int Sec)  
  421.         {  
  422.             TimeSpan ts = DateTime.Now - DateTime.Parse(Time).AddSeconds(Sec);  
  423.             if (ts.TotalSeconds > int.MaxValue)  
  424.             {  
  425.                 return int.MaxValue;  
  426.             }  
  427.             else if (ts.TotalSeconds < int.MinValue)  
  428.             {  
  429.                 return int.MinValue;  
  430.             }  
  431.             return (int)ts.TotalSeconds;  
  432.         }  
  433.   
  434.         /**/  
  435.         /// <summary>  
  436.         /// 返回相差的分钟数  
  437.         /// </summary>  
  438.         /// <param name="time"></param>  
  439.         /// <param name="minutes"></param>  
  440.         /// <returns></returns>  
  441.         public static int StrDateDiffMinutes(string time, int minutes)  
  442.         {  
  443.             if (time == "" || time == null)  
  444.                 return 1;  
  445.             TimeSpan ts = DateTime.Now - DateTime.Parse(time).AddMinutes(minutes);  
  446.             if (ts.TotalMinutes > int.MaxValue)  
  447.             {  
  448.                 return int.MaxValue;  
  449.             }  
  450.             else if (ts.TotalMinutes < int.MinValue)  
  451.             {  
  452.                 return int.MinValue;  
  453.             }  
  454.             return (int)ts.TotalMinutes;  
  455.         }  
  456.   
  457.         /**/  
  458.         /// <summary>  
  459.         /// 返回相差的小时数  
  460.         /// </summary>  
  461.         /// <param name="time"></param>  
  462.         /// <param name="hours"></param>  
  463.         /// <returns></returns>  
  464.         public static int StrDateDiffHours(string time, int hours)  
  465.         {  
  466.             if (time == "" || time == null)  
  467.                 return 1;  
  468.             TimeSpan ts = DateTime.Now - DateTime.Parse(time).AddHours(hours);  
  469.             if (ts.TotalHours > int.MaxValue)  
  470.             {  
  471.                 return int.MaxValue;  
  472.             }  
  473.             else if (ts.TotalHours < int.MinValue)  
  474.             {  
  475.                 return int.MinValue;  
  476.             }  
  477.             return (int)ts.TotalHours;  
  478.         }  
  479.  
  480.         #endregion  
  481.  
  482.         #region file类  
  483.         /**/  
  484.         /// <summary>  
  485.         /// 文件是否存在  
  486.         /// </summary>  
  487.         /// <param name="filePath">相对路径</param>  
  488.         /// <returns></returns>  
  489.         public static bool FileExists(string filePath)  
  490.         {  
  491.             if (string.IsNullOrEmpty(filePath))  
  492.                 return false;  
  493.             filePath = HttpContext.Current.Server.MapPath(filePath);  
  494.             DirectoryInfo dirInfo = new DirectoryInfo(filePath);  
  495.             if (dirInfo.Exists)  
  496.                 return true;  
  497.             return false;  
  498.         }  
  499.  
  500.         #endregion  
  501.     }  
  502.   
posted @ 2016-09-19 16:07  jett010  阅读(311)  评论(0编辑  收藏  举报