类型的安全转换

public class StringUtils{/// /// 获取URL内容 UTF8编码/// /// URL地址/// public static string GetContent(string ContentURL){try{Encoding enc = Encoding.UTF8;//Encoding enc = Encoding.Default;Uri uri = new Uri(ContentURL);HttpWebRequest hwreq = (HttpWebRequest)WebRequest.Create(uri);HttpWebResponse hwrsp = (HttpWebResponse)hwreq.GetResponse();byte[] bts = new byte[(int)hwrsp.ContentLength];Stream s = hwrsp.GetResponseStream();for (int i = 0; i < bts.Length; ){i += s.Read(bts, i, bts.Length - i);}string content = enc.GetString(bts);return content;}catch (Exception ex){return "";}}/// /// 编码 默认编码/// /// /// public static string GBKUrlEncode(string k){return System.Web.HttpUtility.UrlEncode(k, System.Text.Encoding.Default);}/// /// 解码 默认编码/// /// /// public static string GBKUrlDecode(string k){return System.Web.HttpUtility.UrlDecode(k, System.Text.Encoding.Default);}/// /// 检查字符串是否是电子邮件地址/// /// /// public static bool IsEmail(string request){if (request == null)return false;Regex regex = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");return regex.IsMatch(request.Trim());}/// /// 值是否为手机号/// /// /// public static bool IsMobile(string request){//Regex reg = new Regex(@"^1(?:3|5|8)\d{9}$");Regex reg = new Regex(@"^1[3,5,8]{1}[0-9]{1}[0-9]{8}$");if (reg.IsMatch(request)){return true;}else{return false;}}/// /// 转为Bool类型/// /// /// 默认值/// public static bool SafeBool(string text, bool defaultValue){bool flag;if (bool.TryParse(text, out flag)){defaultValue = flag;}return defaultValue;}/// /// 转为时间类型/// /// /// 默认值/// public static DateTime SafeDateTime(string text, DateTime defaultValue){DateTime time;if (DateTime.TryParse(text, out time)){defaultValue = time;}return defaultValue;}/// /// 转为Decimal类型/// /// /// 默认值/// public static decimal SafeDecimal(string text, decimal defaultValue){decimal num;if (decimal.TryParse(text, out num)){defaultValue = num;}return defaultValue;}/// /// 转为Int类型/// /// /// public static int SafeInt(string text){return SafeInt(text, 0);}/// /// 转为Int类型/// /// /// 默认值/// public static int SafeInt(string text, int defaultValue){int num;if (int.TryParse(text, out num)){defaultValue = num;}return defaultValue;}/// /// 转为short类型/// /// /// 默认值/// public static short SafeShort(string text, short defaultValue){short num;if (short.TryParse(text, out num)){defaultValue = num;}return defaultValue;}/// /// 转为字符串/// /// /// public static string SafeStr(string text){return SafeStr(text, "");}/// /// 转为字符串/// /// /// 默认值/// public static string SafeStr(string text, string defaultValue){if (String.IsNullOrEmpty(text)){return defaultValue;}return text.ToString();}/// /// 替换不安全字符/// /// /// public static string SafeCode(string Str){string text1 = "" + Str;if (text1 != ""){text1 = text1.Replace("<", "<");text1 = text1.Replace(">", ">");//text1 = text1.Replace(",", ",");text1 = text1.Replace("'", "‘");text1 = text1.Replace("\"", """);text1 = text1.Replace("update", "");text1 = text1.Replace("insert", "");text1 = text1.Replace("delete", "");text1 = text1.Replace("--", "");text1 = text1.Replace("%", "");text1 = text1.Replace(";", "");//text1 = text1.Replace(",", "");text1 = text1.Replace("alert", "");text1 = text1.Replace("javascript", "");}return text1;}/// /// 组合数组/// /// /// /// public static string ToDelimitedString(ICollection collection, string delimiter){if (collection == null){return string.Empty;}StringBuilder builder = new StringBuilder();if (collection is Hashtable){foreach (object obj2 in ((Hashtable)collection).Keys){builder.Append(obj2.ToString() + delimiter);}}if (collection is ArrayList){foreach (object obj3 in (ArrayList)collection){builder.Append(obj3.ToString() + delimiter);}}if (collection is string[]){foreach (string str in (string[])collection){builder.Append(str + delimiter);}}if (collection is MailAddressCollection){foreach (MailAddress address in (MailAddressCollection)collection){builder.Append(address.Address + delimiter);}}return builder.ToString().TrimEnd(new char[] { Convert.ToChar(delimiter, CultureInfo.InvariantCulture) });}/// /// 返回整数/// /// /// public static int GetDbInt(object obj){if (obj == null || obj == System.DBNull.Value || obj.ToString() == ""){return 0;}else{return int.Parse(obj.ToString());}}/// /// 返回整数/// /// /// public static int GetDbInt(string obj){int temp;if (string.IsNullOrEmpty(obj)){return 0;}else if (!int.TryParse(obj.Replace(",", ""), out temp)){return 0;}return int.Parse(obj.Replace(",", ""));}/// /// 返回字符串/// /// /// public static string GetDbString(object obj){if (obj == null || obj == System.DBNull.Value){return String.Empty;}else{return obj.ToString().Trim();}}/// /// 返回日期/// /// /// public static DateTime GetDbDateTime(object obj){if (obj == null || obj == System.DBNull.Value){return DateTime.MinValue;}else{return Convert.ToDateTime(obj);}}/// /// 返回Decimal类型/// /// /// public static Decimal GetDbDecimal(object obj){if (obj == null || obj == System.DBNull.Value || obj.ToString() == ""){return 0;}else{string tempd = obj.ToString().TrimEnd('%');return decimal.Parse(tempd);}}/// /// 返回Decimal类型/// /// /// 默认值/// public static Decimal GetDbDecimal(object obj, decimal defaultValue){if (obj == null || obj == System.DBNull.Value || obj.ToString() == ""){return defaultValue;}else{string tempd = obj.ToString();return decimal.Parse(tempd);}}/// /// 返回Decimal类型/// /// /// public static Decimal GetDbDecimal(string obj){decimal dec;if (obj == null || !decimal.TryParse(obj, out dec)){return 0;}else{return dec;}}/// /// 返回长整型/// /// /// public static long GetDbLong(object obj){if (obj == null || obj == System.DBNull.Value){return 0;}else{long lId = 0;long.TryParse(obj.ToString(), out lId);return lId;}}/// /// 返回日期/// /// /// public static DateTime GetDateTime(string date){DateTime dt;if (DateTime.TryParse(date, out dt))return dt;return DateTime.MinValue;}/// /// 返回日期/// /// /// public static DateTime GetDateTime(object obj){DateTime dt;if (obj == null){return DateTime.MinValue;}else{if (DateTime.TryParse(obj.ToString(), out dt)){return dt;}else{return DateTime.MinValue;}}}/// /// 截取字符串 按字节计算/// /// /// /// 0 添加.../// public static string TrimByteStr(object obj, int nBytes, int type){nBytes = nBytes * 2;if (obj == null){return "";}if (nBytes <= 0)return "";if (nBytes % 2 != 0)nBytes++;byte[] blist = System.Text.Encoding.GetEncoding("Gb2312").GetBytes(obj.ToString());if (blist.Length > nBytes){if (type == 0){return System.Text.Encoding.GetEncoding("Gb2312").GetString(blist, 0, nBytes).Replace("?", "") + "...";}else{return System.Text.Encoding.GetEncoding("Gb2312").GetString(blist, 0, nBytes).Replace("?", "");}}else{return obj.ToString();}/*if (nBytes > blist.Length)nBytes = blist.Length;if (type == 0){return System.Text.Encoding.GetEncoding("Gb2312").GetString(blist, 0, nBytes) + "...";}else{return System.Text.Encoding.GetEncoding("Gb2312").GetString(blist, 0, nBytes);}*/}/// /// 截取字符串/// /// /// /// 0 添加.../// public static string TrimStr(object obj, int len, int type){if (obj != null){if (obj.ToString().Length > len){if (type == 0){return obj.ToString().Substring(0, len) + "...";}else{return obj.ToString().Substring(0, len);}}else{return obj.ToString();}}else{return "";}}/// /// 返回本对象的Json序列化/// /// /// public static string ToJson(object obj){JavaScriptSerializer serializer = new JavaScriptSerializer();return serializer.Serialize(obj);}/// /// 截取EMAIL/// /// /// public static string SetReviewEmail(string email){string[] arremail = email.Split('@');string tempstr = "";try{tempstr = arremail[0].Substring(0, arremail[0].Length - 2) + "**" + "@**";tempstr += arremail[1].Substring(2);}catch{tempstr = email;}return tempstr;}/// /// 去除HTML代码/// /// /// public static string StripHT(object strHtml){Regex regex = new Regex("<.+?>", RegexOptions.IgnoreCase);string strOutput = regex.Replace(strHtml.ToString(), "");return strOutput.Replace(" ", "");}/// /// 检查字符串是否是日期/// /// /// public static bool IsDateTimeAvailable(string request){return IsDateTimeAvailable(request, DateTime.MinValue, DateTime.MaxValue);}/// /// 检查字符串是否是指定的日期/// /// /// /// /// public static bool IsDateTimeAvailable(string request, DateTime minValue, DateTime maxValue){DateTime req_Time;try{req_Time = DateTime.Parse(request);}catch{return false;}if (req_Time > maxValue || req_Time < minValue)return false;return true;}/// /// 验证身份证号/// /// /// public static bool IsUserIdentityNumAvailable(string id){if (id == null)return false;id = id.Trim();Match match = Regex.Match(id, @"\d{18}|\d{17}\w");if (!match.Success)match = Regex.Match(id, @"\d{15}");if (!match.Success)return false;if (match.Value != id)return false;return true;}}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值