From c52cc07717fd4b41b7156601aa7d05be6196ee6b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=8A=89=E4=BA=9E=E5=9D=A4?= <13140659949@163.com>
Date: Tue, 6 Dec 2022 14:57:18 +0800
Subject: [PATCH] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E4=B8=80=E4=BA=9B=E6=96=B9?=
=?UTF-8?q?=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
SageTools/Extension/DateTime.cs | 4 +
SageTools/Extension/Regex.cs | 77 +++++++++++
SageTools/Extension/String.cs | 222 +++-----------------------------
3 files changed, 96 insertions(+), 207 deletions(-)
create mode 100644 SageTools/Extension/Regex.cs
diff --git a/SageTools/Extension/DateTime.cs b/SageTools/Extension/DateTime.cs
index 28dcd5d..dcd7f58 100644
--- a/SageTools/Extension/DateTime.cs
+++ b/SageTools/Extension/DateTime.cs
@@ -185,5 +185,9 @@ public static DateTime ToDateTime(this long timeStamp)
return new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(timeStamp);
}
+ ///
+ /// 转换为日期格式
+ ///
+ public static DateTime ToDateTime(this string @this) => Convert.ToDateTime(@this);
}
}
\ No newline at end of file
diff --git a/SageTools/Extension/Regex.cs b/SageTools/Extension/Regex.cs
new file mode 100644
index 0000000..b839047
--- /dev/null
+++ b/SageTools/Extension/Regex.cs
@@ -0,0 +1,77 @@
+using System;
+using System.Text.RegularExpressions;
+
+namespace SageTools.Extension
+{
+ public static partial class Extension
+ {
+ ///
+ /// Regex.IsMatch()拓展
+ ///
+ public static bool IsMatch(this string input, string pattern) => Regex.IsMatch(input, pattern);
+
+ ///
+ /// Regex.IsMatch()拓展
+ ///
+ public static bool IsMatch(this string input, string pattern, RegexOptions options) => Regex.IsMatch(input, pattern, options);
+
+ ///
+ /// 正则判断是否为数字
+ ///
+ public static bool IsNumeric(this string @this) => !Regex.IsMatch(@this, "[^0-9]");
+
+ ///
+ /// 是否是正确的base64字符串
+ ///
+ public static bool IsValidBase64String(this string str) => Regex.IsMatch(str, "[A-Za-z0-9\\+\\/\\=]");
+
+ ///
+ /// 是否是正确的email地址
+ ///
+ public static bool IsValidEmail(this string obj) =>
+ Regex.IsMatch(obj, "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
+
+ ///
+ /// 是否是正确的ip地址
+ ///
+ public static bool IsValidIP(this string obj) => Regex.IsMatch(obj,
+ "^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$");
+
+ ///
+ /// 是否是正确的手机号
+ ///
+ public static bool IsValidMobile(string mobile)
+ {
+ if (mobile.IsNullOrEmpty())
+ return false;
+ mobile = mobile.Trim();
+ return Regex.IsMatch(mobile, "^(1[3|4|5|6|7|8|9])\\d{9}$", RegexOptions.IgnoreCase);
+ }
+
+ ///
+ /// 是否是安全的SQL,防注入
+ ///
+ public static bool IsValidSafeSqlString(this string str) => !Regex.IsMatch(str, "[-|;|,|\\/|\\(|\\)|\\[|\\]|\\}|\\{|%|@|\\*|!|\\']");
+
+ ///
+ /// 是否是URL
+ ///
+ public static bool IsUrl(this string strUrl) => Regex.IsMatch(strUrl,
+ "^(http|https)\\://([a-zA-Z0-9\\.\\-]+(\\:[a-zA-Z0-9\\.&%\\$\\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\\-]+\\.)*[a-zA-Z0-9\\-]+\\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\\:[0-9]+)*(/($|[a-zA-Z0-9\\.\\,\\?\\'\\\\\\+&%\\$#\\=~_\\-]+))*$");
+
+ ///
+ /// 是否为弱密码
+ /// 注:密码必须包含数字、小写字母、大写字母和其他符号中的两种并且长度大于8
+ ///
+ /// 密码
+ ///
+ public static bool IsWeakPwd(this string pwd)
+ {
+ if (pwd.IsNullOrEmpty())
+ throw new Exception("pwd不能为空");
+
+ const string pattern = "(^[0-9]+$)|(^[a-z]+$)|(^[A-Z]+$)|(^.{0,8}$)";
+ return Regex.IsMatch(pwd, pattern);
+ }
+ }
+}
\ No newline at end of file
diff --git a/SageTools/Extension/String.cs b/SageTools/Extension/String.cs
index 6105c07..3844743 100644
--- a/SageTools/Extension/String.cs
+++ b/SageTools/Extension/String.cs
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Data;
-using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
@@ -14,33 +13,6 @@ namespace SageTools.Extension
{
public static partial class Extension
{
- ///
- /// 从字符串中截取指定长度
- ///
- ///
- /// 指定的长度
- /// 当指定的长度超出或小于0时,True返回整体字符串(默认),False抛出IndexOutOfRange异常
- ///
- public static string SubSpecifiedLengthStr(this string str, int length, bool returnAllOrThrowWhenIndexOutOfRange = true)
- {
- if (str.IsNullOrEmpty())
- {
- return str;
- }
-
- if (length == 0)
- {
- return string.Empty;
- }
-
- if (length < 0 || length > str.Length)
- {
- return returnAllOrThrowWhenIndexOutOfRange ? str : throw new IndexOutOfRangeException();
- }
-
- return str.Substring(length);
- }
-
public static bool IsNullOrEmpty(this string str)
{
return string.IsNullOrEmpty(str);
@@ -84,39 +56,34 @@ public static bool ToBool(this string str)
///
/// string.Join()拓展
///
- public static string Join(this string separator, string[] value) => string.Join(separator, value);
+ public static string JoinWith(this string separator, string[] value) => string.Join(separator, value);
///
/// string.Join()拓展
///
- public static string Join(this string separator, object[] values) => string.Join(separator, values);
+ public static string JoinWith(this string separator, object[] values) => string.Join(separator, values);
///
/// string.Join()拓展
///
- public static string Join(this string separator, IEnumerable values) => string.Join(separator, values);
+ public static string JoinWith(this string separator, IEnumerable values) => string.Join(separator, values);
///
/// string.Join()拓展
///
- public static string Join(this string separator, IEnumerable values) => string.Join(separator, values);
+ public static string JoinWith(this string separator, IEnumerable values) => string.Join(separator, values);
///
/// string.Join()拓展
///
- public static string Join(this string separator, string[] value, int startIndex, int count) => string.Join(separator, value, startIndex, count);
+ public static string JoinWith(this string separator, string[] value, int startIndex, int count) => string.Join(separator, value, startIndex, count);
///
/// string.Replace("oldValue","")
///
public static string ReplaceByEmpty(this string str, params string[] values)
{
- foreach (var oldValue in values)
- {
- str = str.Replace(oldValue, "");
- }
-
- return str;
+ return values.Aggregate(str, (current, oldValue) => current.Replace(oldValue, ""));
}
///
@@ -314,74 +281,6 @@ public static string Cut(this string @this, int maxLength, string suffix)
return @this.Substring(0, length) + suffix;
}
- ///
- /// 进行MD5加密
- ///
- ///
- ///
- public static string Md5(this string str)
- {
- byte[] hash = new MD5CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(str));
- string str1 = "";
- for (int index = 0; index < hash.Length; ++index)
- str1 += hash[index].ToString("x").PadLeft(2, '0');
- return str1;
- }
-
- ///
- /// Regex.IsMatch()拓展
- ///
- public static bool IsMatch(this string input, string pattern) => Regex.IsMatch(input, pattern);
-
- ///
- /// Regex.IsMatch()拓展
- ///
- public static bool IsMatch(this string input, string pattern, RegexOptions options) => Regex.IsMatch(input, pattern, options);
-
- ///
- /// 正则判断是否为数字
- ///
- public static bool IsNumeric(this string @this) => !Regex.IsMatch(@this, "[^0-9]");
-
- ///
- /// 是否是正确的base64字符串
- ///
- public static bool IsValidBase64String(this string str) => Regex.IsMatch(str, "[A-Za-z0-9\\+\\/\\=]");
-
- ///
- /// 是否是正确的email地址
- ///
- public static bool IsValidEmail(this string obj) =>
- Regex.IsMatch(obj, "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");
-
- ///
- /// 是否是正确的ip地址
- ///
- public static bool IsValidIP(this string obj) => Regex.IsMatch(obj,
- "^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$");
-
- ///
- /// 是否是正确的手机号
- ///
- public static bool IsValidMobile(string mobile)
- {
- if (mobile.IsNullOrEmpty())
- return false;
- mobile = mobile.Trim();
- return Regex.IsMatch(mobile, "^(1[3|4|5|6|7|8|9])\\d{9}$", RegexOptions.IgnoreCase);
- }
-
- ///
- /// 是否是安全的SQL,防注入
- ///
- public static bool IsValidSafeSqlString(this string str) => !Regex.IsMatch(str, "[-|;|,|\\/|\\(|\\)|\\[|\\]|\\}|\\{|%|@|\\*|!|\\']");
-
- ///
- /// 是否是URL
- ///
- public static bool IsUrl(this string strUrl) => Regex.IsMatch(strUrl,
- "^(http|https)\\://([a-zA-Z0-9\\.\\-]+(\\:[a-zA-Z0-9\\.&%\\$\\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\\-]+\\.)*[a-zA-Z0-9\\-]+\\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\\:[0-9]+)*(/($|[a-zA-Z0-9\\.\\,\\?\\'\\\\\\+&%\\$#\\=~_\\-]+))*$");
-
///
/// 满足条件则添加
///
@@ -448,11 +347,6 @@ public static StringBuilder AppendLineFormat(this StringBuilder @this, string fo
///
public static string Right(this string @this, int length) => @this.RightSafe(length);
- ///
- /// 转换为日期格式
- ///
- public static DateTime ToDateTime(this string @this) => Convert.ToDateTime(@this);
-
///
/// 转为字节数组
///
@@ -468,7 +362,7 @@ public static byte[] ToBytes_FromBase64Str(this string base64Str)
///
///
///
- public static string ToMD5String(this string str)
+ public static string ToMd5String(this string str)
{
var md5 = MD5.Create();
var inputBytes = Encoding.UTF8.GetBytes(str);
@@ -490,9 +384,9 @@ public static string ToMD5String(this string str)
///
///
///
- public static string ToMD5String16(this string str)
+ public static string ToMd5String16(this string str)
{
- return str.ToMD5String().Substring(8, 16);
+ return str.ToMd5String().Substring(8, 16);
}
///
@@ -606,7 +500,7 @@ public static string Base64UrlDecode(this string base64UrlStr)
///
public static byte[] ToSHA1Bytes(this string str)
{
- return str.ToSHA1Bytes(Encoding.UTF8);
+ return str.ToSha1Bytes(Encoding.UTF8);
}
///
@@ -615,7 +509,7 @@ public static byte[] ToSHA1Bytes(this string str)
/// 字符串
/// 编码
///
- public static byte[] ToSHA1Bytes(this string str, Encoding encoding)
+ public static byte[] ToSha1Bytes(this string str, Encoding encoding)
{
SHA1 sha1 = new SHA1CryptoServiceProvider();
var inputBytes = encoding.GetBytes(str);
@@ -630,7 +524,7 @@ public static byte[] ToSHA1Bytes(this string str, Encoding encoding)
///
/// 字符串
///
- public static string ToSHA1String(this string str)
+ public static string ToSha1String(this string str)
{
return str.ToSHA1String(Encoding.UTF8);
}
@@ -643,7 +537,7 @@ public static string ToSHA1String(this string str)
///
public static string ToSHA1String(this string str, Encoding encoding)
{
- var sha1Bytes = str.ToSHA1Bytes(encoding);
+ var sha1Bytes = str.ToSha1Bytes(encoding);
var resStr = BitConverter.ToString(sha1Bytes);
return resStr.Replace("-", "").ToLower();
}
@@ -683,33 +577,6 @@ public static string ToHMACSHA256String(this string text, string secret)
return Convert.ToBase64String(hashmessage).Replace('+', '-').Replace('/', '_').TrimEnd('=');
}
- ///
- /// string转int
- ///
- /// 字符串
- ///
- public static int ToInt(this string str)
- {
- str = str.Replace("\0", "");
- if (string.IsNullOrEmpty(str))
- return 0;
- return Convert.ToInt32(str);
- }
-
- ///
- /// string转long
- ///
- /// 字符串
- ///
- public static long ToLong(this string str)
- {
- str = str.Replace("\0", "");
- if (string.IsNullOrEmpty(str))
- return 0;
-
- return Convert.ToInt64(str);
- }
-
///
/// 二进制字符串转为Int
///
@@ -725,21 +592,6 @@ public static int ToInt_FromBinString(this string str)
///
/// 数值
///
- public static int ToInt0X(this string str)
- {
- int num = Int32.Parse(str, NumberStyles.HexNumber);
- return num;
- }
-
- ///
- /// 转换为double
- ///
- /// 字符串
- ///
- public static double ToDouble(this string str)
- {
- return Convert.ToDouble(str);
- }
///
/// string转byte[]
@@ -762,34 +614,6 @@ public static byte[] ToBytes(this string str, Encoding theEncoding)
return theEncoding.GetBytes(str);
}
- ///
- /// 将16进制字符串转为Byte数组
- ///
- /// 16进制字符串(2个16进制字符表示一个Byte)
- ///
- public static byte[] To0XBytes(this string str)
- {
- List resBytes = new List();
- for (int i = 0; i < str.Length; i = i + 2)
- {
- string numStr = $@"{str[i]}{str[i + 1]}";
- resBytes.Add((byte)numStr.ToInt0X());
- }
-
- return resBytes.ToArray();
- }
-
- ///
- /// 将ASCII码形式的字符串转为对应字节数组
- /// 注:一个字节一个ASCII码字符
- ///
- /// 字符串
- ///
- public static byte[] ToASCIIBytes(this string str)
- {
- return str.ToList().Select(x => (byte)x).ToArray();
- }
-
///
/// 删除Json字符串中键中的@符号
///
@@ -829,7 +653,7 @@ public static T ToEntity(this string json)
break;
case "System.Int32":
- info?.SetValue(obj, match.Groups[1].ToString().ToInt(), null);
+ info?.SetValue(obj, match.Groups[1].ToString().ToInt32(), null);
break;
case "System.Int64":
@@ -877,9 +701,8 @@ public static IPEndPoint ToIPEndPoint(this string str)
try
{
var strArray = str.Split(':').ToArray();
- var addr = strArray[0];
var port = Convert.ToInt32(strArray[1]);
- iPEndPoint = new IPEndPoint(IPAddress.Parse(addr), port);
+ iPEndPoint = new IPEndPoint(IPAddress.Parse(strArray[0]), port);
}
catch
{
@@ -901,20 +724,5 @@ public static TEnum ToEnum(this string enumText) where TEnum : struct
return value;
}
-
- ///
- /// 是否为弱密码
- /// 注:密码必须包含数字、小写字母、大写字母和其他符号中的两种并且长度大于8
- ///
- /// 密码
- ///
- public static bool IsWeakPwd(this string pwd)
- {
- if (pwd.IsNullOrEmpty())
- throw new Exception("pwd不能为空");
-
- const string pattern = "(^[0-9]+$)|(^[a-z]+$)|(^[A-Z]+$)|(^.{0,8}$)";
- return Regex.IsMatch(pwd, pattern);
- }
}
}
\ No newline at end of file