string类型扩展

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");
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值