这个工具类,主要是包含一些数字和字符串转换,Json转换等等
using System; using System.Collections; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text.RegularExpressions; using System.Web; using Newtonsoft.Json; namespace SEAEYE.WEB2._0.component { /// <summary> /// CreateTime:2017-06-17 /// Author: /// Desc: 工具类 /// </summary> public static class Common { /// <summary> /// 获取session的用户信息 /// </summary> /// <returns></returns> public static T GetSession<T>(string key) where T : new() { T t = new T(); if (HttpContext.Current.Session[key] != null) t = (T)HttpContext.Current.Session[key]; return t; } /// <summary> /// 转换成 json /// </summary> /// <param name="obj"></param> /// <returns></returns> public static string ToJson(this object obj) { return JsonConvert.SerializeObject(obj); } /// <summary> /// string 数组 转 int /// </summary> /// <param name="strs"></param> /// <returns></returns> public static int[] StrArrToIntArr(string[] strs) { return Array.ConvertAll<string, int>(strs, delegate (string s) { return int.Parse(s); }); } /// <summary> /// string型转换为int型 /// </summary> /// <param name="strValue">要转换的字符串</param> /// <returns>转换后的int类型结果.如果要转换的字符串是非数字,则返回0.</returns> public static int StrToInt(object strValue) { return StrToInt(strValue, 0); } /// <summary> /// string型转换为int型 /// </summary> /// <param name="strValue">要转换的字符串</param> /// <param name="defValue">缺省值</param> /// <returns>转换后的int类型结果</returns> public static int StrToInt(object strValue, int defValue) { if (strValue == null || Convert.IsDBNull(strValue) || strValue.ToString() == string.Empty) { return defValue; } string val = strValue.ToString(); if (val.IndexOf(".", StringComparison.Ordinal) >= 0) { val = val.Split('.')[0]; } int intValue; if (int.TryParse(val, out intValue)) { return intValue; } return defValue; } /// <summary> /// 过滤标签,正则匹配时使用非贪婪模式 /// </summary> /// <param name="conStr">待处理的文本数据</param> /// <param name="tagName">标签名称如,html,Script等</param> /// <param name="fType">过滤方式,可以取(1|2|3) /// 1:是单个标签如img等, /// 2:表示配对出现的标签如div等将清除此标签以及标签内的全部文本, /// 3:表示也是针对配对出现的标签,但是保留标签内的内容. /// </param> /// <returns></returns> public static string CollectionFilter(string conStr, string tagName, int fType) { if (string.IsNullOrEmpty(conStr)) return string.Empty; string input = conStr; switch (fType) { case 1: return Regex.Replace(input, "<" + tagName + "([^>])*>", "", RegexOptions.IgnoreCase); case 2: return Regex.Replace(input, "<" + tagName + "([^>])*>.*?</" + tagName + "([^>])*>", "", RegexOptions.IgnoreCase); case 3: return Regex.Replace(Regex.Replace(input, "<" + tagName + "([^>])*>", "", RegexOptions.IgnoreCase), "</" + tagName + "([^>])*>", "", RegexOptions.IgnoreCase); } return input; } /// <summary> /// <summary> /// 自定义的替换字符串函数 /// </summary> /// </summary> /// <param name="sourceString"></param> /// <param name="searchString"></param> /// <param name="replaceString"></param> /// <param name="isCaseInsensetive">true 为指定不区分大小写的匹配。</param> /// <returns></returns> public static string ReplaceString(string sourceString, string searchString, string replaceString, bool isCaseInsensetive) { return Regex.Replace(sourceString, Regex.Escape(searchString), replaceString, isCaseInsensetive ? RegexOptions.IgnoreCase : RegexOptions.None); } /// <summary> /// 随机文件名称 /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static string GetRandomFileName(string fileName) { if (string.IsNullOrEmpty(fileName)) return ""; System.Threading.Thread.Sleep(100); string fileType = fileName.Substring(fileName.LastIndexOf(".", StringComparison.Ordinal)); #region 暂时弃用 //var rand = new Random(); //DateTime now = DateTime.Now; //string str = ""; //str += now.Year.ToString(); //str += now.Month.ToString(); //str += now.Day.ToString(); //str += now.Hour.ToString(); //str += now.Minute.ToString(); //str += now.Second.ToString(); //str += rand.Next(0, 1000); #endregion Guid guid = Guid.NewGuid(); return guid + fileType; } /// <summary> /// 随机文件名称 /// </summary> /// <param name="fileName"></param> /// <param name="nguid"></param> /// <returns></returns> public static string GetRandomFileName(string fileName,out string nguid) { nguid = string.Empty; if (string.IsNullOrEmpty(fileName)) return ""; System.Threading.Thread.Sleep(100); string fileType = fileName.Substring(fileName.LastIndexOf(".", StringComparison.Ordinal)); #region 暂时弃用 //var rand = new Random(); //DateTime now = DateTime.Now; //string str = ""; //str += now.Year.ToString(); //str += now.Month.ToString(); //str += now.Day.ToString(); //str += now.Hour.ToString(); //str += now.Minute.ToString(); //str += now.Second.ToString(); //str += rand.Next(0, 1000); #endregion Guid guid = Guid.NewGuid(); nguid = guid.ToString(); return guid + fileType; } /// <summary> /// string list -> int list /// </summary> /// <param name="list"></param> /// <returns></returns> public static IList<int> ToIntList(this IList<string> list) { return list.Select(int.Parse).ToList(); } /// <summary> /// int list -> string list /// </summary> /// <param name="list"></param> /// <returns></returns> public static IList<string> ToStringList(this IList<string> list) { return list.Select(x=>x.ToString(CultureInfo.InvariantCulture)).ToList(); } } }
using System; using System.Collections; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text.RegularExpressions; using System.Web; using Newtonsoft.Json; namespace SEAEYE.WEB2._0.component { /// <summary> /// CreateTime:2017-06-17 /// Author: /// Desc: 工具类 /// </summary> public static class Common { /// <summary> /// 获取session的用户信息 /// </summary> /// <returns></returns> public static T GetSession<T>(string key) where T : new() { T t = new T(); if (HttpContext.Current.Session[key] != null) t = (T)HttpContext.Current.Session[key]; return t; } /// <summary> /// 转换成 json /// </summary> /// <param name="obj"></param> /// <returns></returns> public static string ToJson(this object obj) { return JsonConvert.SerializeObject(obj); } /// <summary> /// string 数组 转 int /// </summary> /// <param name="strs"></param> /// <returns></returns> public static int[] StrArrToIntArr(string[] strs) { return Array.ConvertAll<string, int>(strs, delegate (string s) { return int.Parse(s); }); } /// <summary> /// string型转换为int型 /// </summary> /// <param name="strValue">要转换的字符串</param> /// <returns>转换后的int类型结果.如果要转换的字符串是非数字,则返回0.</returns> public static int StrToInt(object strValue) { return StrToInt(strValue, 0); } /// <summary> /// string型转换为int型 /// </summary> /// <param name="strValue">要转换的字符串</param> /// <param name="defValue">缺省值</param> /// <returns>转换后的int类型结果</returns> public static int StrToInt(object strValue, int defValue) { if (strValue == null || Convert.IsDBNull(strValue) || strValue.ToString() == string.Empty) { return defValue; } string val = strValue.ToString(); if (val.IndexOf(".", StringComparison.Ordinal) >= 0) { val = val.Split('.')[0]; } int intValue; if (int.TryParse(val, out intValue)) { return intValue; } return defValue; } /// <summary> /// 过滤标签,正则匹配时使用非贪婪模式 /// </summary> /// <param name="conStr">待处理的文本数据</param> /// <param name="tagName">标签名称如,html,Script等</param> /// <param name="fType">过滤方式,可以取(1|2|3) /// 1:是单个标签如img等, /// 2:表示配对出现的标签如div等将清除此标签以及标签内的全部文本, /// 3:表示也是针对配对出现的标签,但是保留标签内的内容. /// </param> /// <returns></returns> public static string CollectionFilter(string conStr, string tagName, int fType) { if (string.IsNullOrEmpty(conStr)) return string.Empty; string input = conStr; switch (fType) { case 1: return Regex.Replace(input, "<" + tagName + "([^>])*>", "", RegexOptions.IgnoreCase); case 2: return Regex.Replace(input, "<" + tagName + "([^>])*>.*?</" + tagName + "([^>])*>", "", RegexOptions.IgnoreCase); case 3: return Regex.Replace(Regex.Replace(input, "<" + tagName + "([^>])*>", "", RegexOptions.IgnoreCase), "</" + tagName + "([^>])*>", "", RegexOptions.IgnoreCase); } return input; } /// <summary> /// <summary> /// 自定义的替换字符串函数 /// </summary> /// </summary> /// <param name="sourceString"></param> /// <param name="searchString"></param> /// <param name="replaceString"></param> /// <param name="isCaseInsensetive">true 为指定不区分大小写的匹配。</param> /// <returns></returns> public static string ReplaceString(string sourceString, string searchString, string replaceString, bool isCaseInsensetive) { return Regex.Replace(sourceString, Regex.Escape(searchString), replaceString, isCaseInsensetive ? RegexOptions.IgnoreCase : RegexOptions.None); } /// <summary> /// 随机文件名称 /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static string GetRandomFileName(string fileName) { if (string.IsNullOrEmpty(fileName)) return ""; System.Threading.Thread.Sleep(100); string fileType = fileName.Substring(fileName.LastIndexOf(".", StringComparison.Ordinal)); #region 暂时弃用 //var rand = new Random(); //DateTime now = DateTime.Now; //string str = ""; //str += now.Year.ToString(); //str += now.Month.ToString(); //str += now.Day.ToString(); //str += now.Hour.ToString(); //str += now.Minute.ToString(); //str += now.Second.ToString(); //str += rand.Next(0, 1000); #endregion Guid guid = Guid.NewGuid(); return guid + fileType; } /// <summary> /// 随机文件名称 /// </summary> /// <param name="fileName"></param> /// <param name="nguid"></param> /// <returns></returns> public static string GetRandomFileName(string fileName,out string nguid) { nguid = string.Empty; if (string.IsNullOrEmpty(fileName)) return ""; System.Threading.Thread.Sleep(100); string fileType = fileName.Substring(fileName.LastIndexOf(".", StringComparison.Ordinal)); #region 暂时弃用 //var rand = new Random(); //DateTime now = DateTime.Now; //string str = ""; //str += now.Year.ToString(); //str += now.Month.ToString(); //str += now.Day.ToString(); //str += now.Hour.ToString(); //str += now.Minute.ToString(); //str += now.Second.ToString(); //str += rand.Next(0, 1000); #endregion Guid guid = Guid.NewGuid(); nguid = guid.ToString(); return guid + fileType; } /// <summary> /// string list -> int list /// </summary> /// <param name="list"></param> /// <returns></returns> public static IList<int> ToIntList(this IList<string> list) { return list.Select(int.Parse).ToList(); } /// <summary> /// int list -> string list /// </summary> /// <param name="list"></param> /// <returns></returns> public static IList<string> ToStringList(this IList<string> list) { return list.Select(x=>x.ToString(CultureInfo.InvariantCulture)).ToList(); } } }