标签:return string C# self 扩展 static ConvertToMD5 public
string扩展方法,收集扩展了一些常用的方法,可直接在工程中使用
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
public static class StringExtension
{
// 移除前缀字符串
public static string RemovePrefixString(this string self, string str)
{
string strRegex = @"^(" + str + ")";
return Regex.Replace(self, strRegex, "");
}
// 移除后缀字符串
public static string RemoveSuffixString(this string self, string str)
{
string strRegex = @"(" + str + ")" + "$";
return Regex.Replace(self, strRegex, "");
}
// 是否为Email
public static bool IsEmail(this string self)
{
return self.RegexMatch(@"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
}
// 是否为域名
public static bool IsDomain(this string self)
{
return self.RegexMatch(@"[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+/.?");
}
// 是否为IP地址
public static bool IsIP(this string self)
{
return self.RegexMatch(@"((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))");
}
// 是否为手机号码
public static bool IsMobilePhone(this string self)
{
return self.RegexMatch(@"^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$");
}
// 匹配正则表达式
public static bool RegexMatch(this string slef, string pattern)
{
Regex reg = new Regex(pattern);
return reg.Match(slef).Success;
}
// 转换为MD5, 加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位
public static string ConvertToMD5(this string self,string flag = "x2")
{
byte[] sor = Encoding.UTF8.GetBytes(self);
MD5 md5 = MD5.Create();
byte[] result = md5.ComputeHash(sor);
StringBuilder strbul = new StringBuilder(40);
for (int i = 0; i < result.Length; i++)
{
strbul.Append(result[i].ToString(flag));
}
return strbul.ToString();
}
// 转换为32位MD5
public static string ConvertToMD5_32(this string self)
{
return ConvertToMD5(self, "x2");
}
// 转换为48位MD5
public static string ConvertToMD5_48(this string self)
{
return ConvertToMD5(self, "x3");
}
// 转换为64位MD5
public static string ConvertToMD5_64(this string self)
{
return ConvertToMD5(self, "x4");
}
}
标签:return,string,C#,self,扩展,static,ConvertToMD5,public 来源: https://blog.csdn.net/qq_30473517/article/details/98657446
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。