Web--Utils
Posted zhaoyl9
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Web--Utils相关的知识,希望对你有一定的参考价值。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Web; namespace Xima.FreamWork.Common.Web public class Utils /// <summary> /// 返回字符串真实长度, 1个汉字长度为2 /// </summary> /// <returns>字符长度</returns> public static int GetStringLength(string str) return Encoding.Default.GetBytes(str).Length; /// <summary> /// 检测是否有Sql危险字符 /// </summary> /// <param name="str">要判断字符串</param> /// <returns>判断结果</returns> public static bool IsSafeSqlString(string str) return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\|\|%|@|\*|!|\‘]"); #region "字符转换" /// <summary> /// 将object转换为string类型 /// </summary> /// <param name="expression"></param> /// <returns></returns> public static string ToStr(object expression) return TypeConverter.ObjToStr(expression); /// <summary> /// object型转换为bool型 /// </summary> /// <param name="strValue">要转换的字符串</param> /// <param name="defValue">缺省值</param> /// <returns>转换后的bool类型结果</returns> public static bool StrToBool(object expression, bool defValue) return TypeConverter.StrToBool(expression, defValue); /// <summary> /// string型转换为bool型 /// </summary> /// <param name="strValue">要转换的字符串</param> /// <param name="defValue">缺省值</param> /// <returns>转换后的bool类型结果</returns> public static bool StrToBool(string expression, bool defValue) return TypeConverter.StrToBool(expression, defValue); /// <summary> /// 将对象转换为Int32类型 /// </summary> /// <param name="expression">要转换的字符串</param> /// <param name="defValue">缺省值</param> /// <returns>转换后的int类型结果</returns> public static int StrToInt(object expression, int defValue) return TypeConverter.ObjectToInt(expression, defValue); /// <summary> /// 将字符串转换为Int32类型 /// </summary> /// <param name="expression">要转换的字符串</param> /// <param name="defValue">缺省值</param> /// <returns>转换后的int类型结果</returns> public static int StrToInt(string expression, int defValue) return TypeConverter.StrToInt(expression, defValue); /// <summary> /// 将字符串转换为Byte类型 /// </summary> /// <param name="expression">要转换的字符串</param> /// <param name="defValue">缺省值</param> /// <returns>转换后的int类型结果</returns> public static Byte StrToByte(object expression, byte defValue) return TypeConverter.ObjectToByte(expression, defValue); /// <summary> /// 将字符串转换为Byte类型 /// </summary> /// <param name="expression">要转换的字符串</param> /// <param name="defValue">缺省值</param> /// <returns>转换后的int类型结果</returns> public static Byte StrToByte(string expression, byte defValue) return TypeConverter.StrToByte(expression, defValue); /// <summary> /// 将字符串转换为Int32类型 /// </summary> /// <param name="expression">要转换的字符串</param> /// <param name="defValue">缺省值</param> /// <returns>转换后的int类型结果</returns> public static Int64 StrToInt64(string expression, int defValue) return TypeConverter.StrToInt64(expression, defValue); /// <summary> /// Object型转换为float型 /// </summary> /// <param name="strValue">要转换的字符串</param> /// <param name="defValue">缺省值</param> /// <returns>转换后的int类型结果</returns> public static float StrToFloat(object strValue, float defValue) return TypeConverter.StrToFloat(strValue, defValue); /// <summary> /// string型转换为float型 /// </summary> /// <param name="strValue">要转换的字符串</param> /// <param name="defValue">缺省值</param> /// <returns>转换后的float类型结果</returns> public static float StrToFloat(string strValue, float defValue) return TypeConverter.StrToFloat(strValue, defValue); /// <summary> /// string类型转换为decimal /// </summary> /// <param name="strValue">要转换的字符串</param> /// <param name="defValue">缺省值</param> /// <returns>转换后的decimal类型结果</returns> public static decimal strToDecimal(string strValue, decimal defValue) return TypeConverter.StrToDecimal(strValue, defValue); #endregion #region "IP地址" /// <summary> /// 是否为ip /// </summary> /// <param name="ip"></param> /// <returns></returns> public static bool IsIP(string ip) return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.)3(2[0-4]\d|25[0-5]|[01]?\d\d?)$"); public static bool IsIPSect(string ip) return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.)2((2[0-4]\d|25[0-5]|[01]?\d\d?|\*)\.)(2[0-4]\d|25[0-5]|[01]?\d\d?|\*)$"); #endregion #region grid文字超长截取 public static string GetUnicodeSubString(string str, int len, string p_TailString) string result = string.Empty;// 最终返回的结果 int byteLen = System.Text.Encoding.Default.GetByteCount(str);// 单字节字符长度 int charLen = str.Length;// 把字符平等对待时的字符串长度 int byteCount = 0;// 记录读取进度 int pos = 0;// 记录截取位置 if (byteLen > len) for (int i = 0; i < charLen; i++) if (Convert.ToInt32(str.ToCharArray()[i]) > 255)// 按中文字符计算加2 byteCount += 2; else// 按英文字符计算加1 byteCount += 1; if (byteCount > len)// 超出时只记下上一个有效位置 pos = i; break; else if (byteCount == len)// 记下当前位置 pos = i + 1; break; if (pos >= 0) result = str.Substring(0, pos) + p_TailString; else result = str; return result; #endregion /// <summary> /// 格式化价格信息 /// </summary> /// <param name="Price"></param> /// <returns></returns> public static string FormatPrice(object Price) return string.Format("0:c", decimal.Parse(Price.ToString())); /// <summary> /// 返回根目录 /// </summary> /// <returns></returns> public static string GetRootPath() return System.Web.HttpContext.Current.Server.MapPath("/"); /// <summary> /// 获得当前绝对路径 /// </summary> /// <param name="strPath">指定的路径</param> /// <returns>绝对路径</returns> public static string GetMapPath(string strPath) if (HttpContext.Current != null) return HttpContext.Current.Server.MapPath(strPath); else //非web程序引用 strPath = strPath.Replace("/", "\\"); if (strPath.StartsWith("\\")) strPath = strPath.Substring(strPath.IndexOf(‘\\‘, 1)).TrimStart(‘\\‘); return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath); #region "按当前日期和时间生成随机数" /// <summary> /// 按当前日期和时间生成随机数 /// </summary> /// <param name="Num">附加随机数长度</param> /// <returns></returns> public static string sRndNum(int Num) string sTmp_Str = System.DateTime.Today.Year.ToString() + System.DateTime.Today.Month.ToString("00") + System.DateTime.Today.Day.ToString("00") + System.DateTime.Now.Hour.ToString("00") + System.DateTime.Now.Minute.ToString("00") + System.DateTime.Now.Second.ToString("00"); return sTmp_Str + RndNum(Num); #endregion #region 生成0-9随机数 /// <summary> /// 生成0-9随机数 /// </summary> /// <param name="VcodeNum">生成长度</param> /// <returns></returns> public static string RndNum(int VcodeNum) StringBuilder sb = new StringBuilder(VcodeNum); Random rand = new Random((int)DateTime.Now.Ticks); for (int i = 1; i < VcodeNum + 1; i++) int t = rand.Next(9); sb.AppendFormat("0", t); return sb.ToString(); #endregion /// <summary> /// 计算两个日期的时间间隔 /// </summary> /// <param name="DateTime1">第一个日期和时间</param> /// <param name="DateTime2">第二个日期和时间</param> /// <returns></returns> public static string DateDiff(DateTime DateTime1, DateTime DateTime2) string dateDiff = string.Empty; TimeSpan ts1 = new TimeSpan(DateTime1.Ticks); TimeSpan ts2 = new TimeSpan(DateTime2.Ticks); TimeSpan ts = ts1.Subtract(ts2).Duration(); dateDiff = ts.Days.ToString() + "Day" + ts.Hours.ToString() + "Hours" + ts.Minutes.ToString() + "Minutes" + ts.Seconds.ToString() + "Seconds"; return dateDiff; /// <summary> /// 计算两个日期的时间间隔 /// 第一个日期减去第二个日期 /// </summary> /// <param name="DateTime1">第一个日期和时间</param> /// <param name="DateTime2">第二个日期和时间</param> /// <returns></returns> public static int Date2Diff(DateTime DateTime1, DateTime DateTime2) DateTime1 = Convert.ToDateTime(DateTime1.ToString("yyyy-MM-dd")); DateTime2 = Convert.ToDateTime(DateTime2.ToString("yyyy-MM-dd")); TimeSpan ts1 = new TimeSpan(DateTime1.Ticks); TimeSpan ts2 = new TimeSpan(DateTime2.Ticks); TimeSpan ts = ts1.Subtract(ts2); return ts.Days; /// <summary> /// 计算日期到今天时间间隔,返回整形 /// </summary> /// <param name="DateTime1">需要计算的日期</param> /// <returns></returns> public static int DateDiff(DateTime DateTime1) int dateDiff = 0; TimeSpan ts1 = new TimeSpan(DateTime1.Ticks); TimeSpan ts2 = new TimeSpan(DateTime.Now.Ticks); TimeSpan ts = ts1.Subtract(ts2).Duration(); dateDiff = ts.Days; return dateDiff; #region 全角半角转换 /// <summary> /// 转全角的函数(SBC case) /// </summary> /// <param name="input">任意字符串</param> /// <returns>全角字符串</returns> ///<remarks> ///全角空格为12288,半角空格为32 ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248 ///</remarks> public static string ToSBC(string input) //半角转全角: char[] c = input.ToCharArray(); for (int i = 0; i < c.Length; i++) if (c[i] == 32) c[i] = (char)12288; continue; if (c[i] < 127) c[i] = (char)(c[i] + 65248); return new string(c); /// <summary> 转半角的函数(DBC case) </summary> /// <param name="input">任意字符串</param> /// <returns>半角字符串</returns> ///<remarks> ///全角空格为12288,半角空格为32 ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248 ///</remarks> public static string ToDBC(string input) char[] c = input.ToCharArray(); for (int i = 0; i < c.Length; i++) if (c[i] == 12288) c[i] = (char)32; continue; if (c[i] > 65280 && c[i] < 65375) c[i] = (char)(c[i] - 65248); return new string(c); /// <summary> /// 把英文半角符号‘"替换为英文全角符号'",防止干扰前台的js函数tooltip.show()而导致乱码 /// </summary> /// <param name="strList">不安全的字符串</param> /// <returns>安全的字符串</returns> public static string protectJsTooltipShow(string strList) string strSafe = strList; strSafe = strSafe.Replace("&", "&"); strSafe = strSafe.Replace("\"", """); strSafe = strSafe.Replace("\n", "<br>"); strSafe = strSafe.Replace(@"\", @"\\"); strSafe = strSafe.Replace("‘", "\\‘"); strSafe = strSafe.Replace("<", "<"); strSafe = strSafe.Replace(">", ">"); return strSafe; #endregion public static string ChineseTONumber(string chStr) const string numStr = "0123456789"; const string chineseStr = "零一二三四五六七八九"; char[] c = chStr.ToCharArray(); for (int i = 0; i < c.Length; i++) int index = chineseStr.IndexOf(c[i]); if (index != -1) c[i] = numStr.ToCharArray()[index]; return new string(c); public static string NumberToChinese(string numberStr) var numStr = "0123456789"; var chineseStr = "零一二三四五六七八九"; char[] c = numberStr.ToCharArray(); for (int i = 0; i < c.Length; i++) int index = numStr.IndexOf(c[i]); if (index != -1) c[i] = chineseStr.ToCharArray()[index]; return new string(c);
以上是关于Web--Utils的主要内容,如果未能解决你的问题,请参考以下文章