123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace Common
- {
- public static class StringExtension
- {
- /// <summary>
- /// 字符空判断
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static bool IsEmpty(this string str)
- {
- if (string.IsNullOrEmpty(str))
- return true;
- return false;
- }
- /// <summary>
- /// 直接格式化字符串
- /// </summary>
- /// <param name="str"></param>
- /// <param name="args"></param>
- /// <returns></returns>
- public static string FormatMe(this string str, params object[] args)
- {
- return string.Format(str, args);
- }
- /// <summary>
- /// 转化为32位int
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static int TryToInt32(this string str)
- {
- return Int32.Parse(str);
- }
- /// <summary>
- /// 转换时间
- /// </summary>
- /// <param name="str"></param>
- /// <returns></returns>
- public static DateTime ToDateTime(this string str)
- {
- return DateTime.Parse(str);
- }
- /// <summary>
- /// MD5加密
- /// </summary>
- /// <param name="myString">加密字符串</param>
- /// <param name="salt">随机加密码</param>
- /// <returns></returns>
- public static string GetMD5(this string str, string salt)
- {
- MD5 md5 = new MD5CryptoServiceProvider();
- byte[] fromData = System.Text.Encoding.Unicode.GetBytes(str + salt);
- byte[] targetData = md5.ComputeHash(fromData);
- string byte2String = null;
- for (int i = 0; i < targetData.Length; i++)
- {
- byte2String += targetData[i].ToString("x");
- }
- return byte2String;
- }
- /// <summary>
- /// 将对象转换为Int32类型,转换失败返回0
- /// </summary>
- /// <param name="str">要转换的字符串</param>
- /// <returns>转换后的int类型结果</returns>
- public static int StrToInt(this string str)
- {
- return StrToInt(str, 0);
- }
- /// <summary>
- /// 将对象转换为Int32类型
- /// </summary>
- /// <param name="str">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的int类型结果</returns>
- public static int StrToInt(this string str, int defValue)
- {
- if (string.IsNullOrEmpty(str) || str.Trim().Length >= 11 || !Regex.IsMatch(str.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
- return defValue;
- int rv;
- if (Int32.TryParse(str, out rv))
- return rv;
- return Convert.ToInt32(StrToFloat(str, defValue));
- }
- /// <summary>
- /// string型转换为float型
- /// </summary>
- /// <param name="strValue">要转换的字符串</param>
- /// <param name="defValue">缺省值</param>
- /// <returns>转换后的int类型结果</returns>
- public static float StrToFloat(this string strValue, float defValue)
- {
- if ((strValue == null) || (strValue.Length > 10))
- return defValue;
- float intValue = defValue;
- if (strValue != null)
- {
- bool IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
- if (IsFloat)
- float.TryParse(strValue, out intValue);
- }
- return intValue;
- }
- /// <summary>
- /// 把特定字符删除
- /// </summary>
- /// <param name="value"></param>
- /// <param name="zf"></param>
- /// <returns></returns>
- public static string RemoveValue(this string value, string zf= "zx95")
- {
- return value.Replace(zf, "");
- }
- /// <summary>
- /// 值是否是存在枚举中
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="value"></param>
- /// <returns></returns>
- public static bool IsEnum<T>(this string value)
- where T : struct
- {
- T a;
- if (Enum.TryParse<T>(value, out a))
- {
- return true;
- }
- return false;
- }
- }
- }
|