{"id":18,"date":"2019-09-08T16:19:51","date_gmt":"2019-09-08T08:19:51","guid":{"rendered":"http:\/\/www.blinkedu.cn\/?p=18"},"modified":"2019-10-05T22:05:51","modified_gmt":"2019-10-05T14:05:51","slug":"stringextension","status":"publish","type":"post","link":"https:\/\/www.blinkedu.cn\/index.php\/2019\/09\/08\/stringextension\/","title":{"rendered":"C#String\u5bf9\u8c61\u65b9\u6cd5\u6269\u5c55"},"content":{"rendered":"<pre><code class=\"language-csharp line-numbers\">using System.Text;\nusing System.Text.RegularExpressions;\n\npublic static class StringExtension\n{\n    \/\/ \u79fb\u9664\u524d\u7f00\u5b57\u7b26\u4e32\n    public static string RemovePrefixString(this string self, string str)\n    {\n        string strRegex = @&quot;^(&quot; + str + &quot;)&quot;;\n        return Regex.Replace(self, strRegex, &quot;&quot;);\n    }\n\n    \/\/ \u79fb\u9664\u540e\u7f00\u5b57\u7b26\u4e32\n    public static string RemoveSuffixString(this string self, string str)\n    {\n        string strRegex = @&quot;(&quot; + str + &quot;)&quot; + &quot;$&quot;;\n        return Regex.Replace(self, strRegex, &quot;&quot;);\n    }\n\n    \/\/ \u662f\u5426\u4e3aEmail\n    public static bool IsEmail(this string self)\n    {\n        return self.RegexMatch(@&quot;^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$&quot;);\n    }\n\n    \/\/ \u662f\u5426\u4e3a\u57df\u540d\n    public static bool IsDomain(this string self)\n    {\n        return self.RegexMatch(@&quot;[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\/.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\/.?&quot;);\n    }\n\n    \/\/ \u662f\u5426\u4e3aIP\u5730\u5740\n    public static bool IsIP(this string self)\n    {\n        return self.RegexMatch(@&quot;((?:(?:25[0-5]|2[0-4]\\\\d|[01]?\\\\d?\\\\d)\\\\.){3}(?:25[0-5]|2[0-4]\\\\d|[01]?\\\\d?\\\\d))&quot;);\n    }\n\n    \/\/ \u662f\u5426\u4e3a\u624b\u673a\u53f7\u7801\n    public static bool IsMobilePhone(this string self)\n    {\n        return self.RegexMatch(@&quot;^(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}$&quot;);\n    }\n\n    \/\/ \u5339\u914d\u6b63\u5219\u8868\u8fbe\u5f0f\n    public static bool RegexMatch(this string slef,  string pattern)\n    {\n        Regex reg = new Regex(pattern);\n        return reg.Match(slef).Success;\n    }\n\n    \/\/ \u8f6c\u6362\u4e3aMD5, \u52a0\u5bc6\u7ed3\u679c&quot;x2&quot;\u7ed3\u679c\u4e3a32\u4f4d,&quot;x3&quot;\u7ed3\u679c\u4e3a48\u4f4d,&quot;x4&quot;\u7ed3\u679c\u4e3a64\u4f4d\n    public static string ConvertToMD5(this string self,string flag = &quot;x2&quot;)\n    {\n        byte[] sor = Encoding.UTF8.GetBytes(self);\n        MD5 md5 = MD5.Create();\n        byte[] result = md5.ComputeHash(sor);\n        StringBuilder strbul = new StringBuilder(40);\n        for (int i = 0; i &lt; result.Length; i++)\n        {\n            strbul.Append(result[i].ToString(flag));\n        }\n        return strbul.ToString();\n    }\n\n    \/\/ \u8f6c\u6362\u4e3a32\u4f4dMD5\n    public static string ConvertToMD5_32(this string self)\n    {\n        return ConvertToMD5(self, &quot;x2&quot;);\n    }\n\n    \/\/ \u8f6c\u6362\u4e3a48\u4f4dMD5\n    public static string ConvertToMD5_48(this string self)\n    {\n        return ConvertToMD5(self, &quot;x3&quot;);\n    }\n\n    \/\/ \u8f6c\u6362\u4e3a64\u4f4dMD5\n    public static string ConvertToMD5_64(this string self)\n    {\n        return ConvertToMD5(self, &quot;x4&quot;);\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>using System.Text; using System.Text.RegularExpressions; public static class StringExtension { \/\/ \u79fb\u9664\u524d\u7f00\u5b57\u7b26\u4e32 public static string RemovePrefixString(this string self, string\u2026\u2026<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[7],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-csharp","tag-c"],"_links":{"self":[{"href":"https:\/\/www.blinkedu.cn\/index.php\/wp-json\/wp\/v2\/posts\/18"}],"collection":[{"href":"https:\/\/www.blinkedu.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.blinkedu.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.blinkedu.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.blinkedu.cn\/index.php\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":1,"href":"https:\/\/www.blinkedu.cn\/index.php\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":85,"href":"https:\/\/www.blinkedu.cn\/index.php\/wp-json\/wp\/v2\/posts\/18\/revisions\/85"}],"wp:attachment":[{"href":"https:\/\/www.blinkedu.cn\/index.php\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blinkedu.cn\/index.php\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blinkedu.cn\/index.php\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}