Files
tnb.server/common/Tnb.CollectiveOAuth/Utils/StringExtensions.cs
2023-03-13 15:00:34 +08:00

224 lines
7.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Text;
using System.Text.RegularExpressions;
namespace JNPF.Extras.CollectiveOAuth.Utils;
/// <summary>
/// 字符串<see cref="string"/>类型的扩展辅助操作类.
/// </summary>
public static class StringExtensions
{
#region
/// <summary>
/// 指示所指定的正则表达式在指定的输入字符串中是否找到了匹配项.
/// </summary>
/// <param name="value">要搜索匹配项的字符串.</param>
/// <param name="pattern">要匹配的正则表达式模式.</param>
/// <returns>如果正则表达式找到匹配项,则为 true否则为 false.</returns>
public static bool IsMatch(this string value, string pattern)
{
if (value == null)
{
return false;
}
return Regex.IsMatch(value, pattern);
}
/// <summary>
/// 在指定的输入字符串中搜索指定的正则表达式的第一个匹配项.
/// </summary>
/// <param name="value">要搜索匹配项的字符串.</param>
/// <param name="pattern">要匹配的正则表达式模式.</param>
/// <returns>一个对象,包含有关匹配项的信息.</returns>
public static string Match(this string value, string pattern)
{
if (value == null)
return null;
return Regex.Match(value, pattern).Value;
}
/// <summary>
/// 在指定的输入字符串中搜索指定的正则表达式的所有匹配项的字符串集合.
/// </summary>
/// <param name="value"> 要搜索匹配项的字符串.</param>
/// <param name="pattern"> 要匹配的正则表达式模式.</param>
/// <returns> 一个集合,包含有关匹配项的字符串值.</returns>
public static IEnumerable<string> Matches(this string value, string pattern)
{
if (value == null)
return new string[] { };
MatchCollection matches = Regex.Matches(value, pattern);
return from Match match in matches select match.Value;
}
/// <summary>
/// 是否电子邮件.
/// </summary>
public static bool IsEmail(this string value)
{
const string pattern = @"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$";
return value.IsMatch(pattern);
}
/// <summary>
/// 是否是IP地址.
/// </summary>
public static bool IsIpAddress(this string value)
{
const string pattern = @"^(\d(25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\d\.){3}\d(25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\d$";
return value.IsMatch(pattern);
}
/// <summary>
/// 是否是整数.
/// </summary>
public static bool IsNumeric(this string value)
{
const string pattern = @"^\-?[0-9]+$";
return value.IsMatch(pattern);
}
/// <summary>
/// 是否是Unicode字符串.
/// </summary>
public static bool IsUnicode(this string value)
{
const string pattern = @"^[\u4E00-\u9FA5\uE815-\uFA29]+$";
return value.IsMatch(pattern);
}
/// <summary>
/// 是否Url字符串.
/// </summary>
public static bool IsUrl(this string value)
{
const string pattern = @"^(http|https|ftp|rtsp|mms):(\/\/|\\\\)[A-Za-z0-9%\-_@]+\.[A-Za-z0-9%\-_@]+[A-Za-z0-9\.\/=\?%\-&_~`@:\+!;]*$";
return value.IsMatch(pattern);
}
/// <summary>
/// 是否身份证号验证如下3种情况
/// 1.身份证号码为15位数字
/// 2.身份证号码为18位数字
/// 3.身份证号码为17位数字+1个字母.
/// </summary>
public static bool IsIdentityCard(this string value)
{
const string pattern = @"^(^\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$";
return value.IsMatch(pattern);
}
/// <summary>
/// 是否手机号码.
/// </summary>
/// <param name="value"></param>
/// <param name="isRestrict">是否按严格格式验证.</param>
public static bool IsMobileNumber(this string value, bool isRestrict = false)
{
string pattern = isRestrict ? @"^[1][3-8]\d{9}$" : @"^[1]\d{10}$";
return value.IsMatch(pattern);
}
#endregion
#region
/// <summary>
/// 指示指定的字符串是 null 还是 System.String.Empty 字符串.
/// </summary>
public static bool IsNullOrEmpty(this string value)
{
return string.IsNullOrEmpty(value);
}
/// <summary>
/// 指示指定的字符串是 null、空还是仅由空白字符组成.
/// </summary>
public static bool IsNullOrWhiteSpace(this string value)
{
return string.IsNullOrWhiteSpace(value);
}
/// <summary>
/// 判断指定路径是否图片文件.
/// </summary>
public static bool IsImageFile(this string filename)
{
if (!File.Exists(filename))
{
return false;
}
byte[] filedata = File.ReadAllBytes(filename);
if (filedata.Length == 0)
return false;
switch (BitConverter.ToUInt16(filedata, 0))
{
case 0x4D42: // bmp
case 0xD8FF: // jpg
case 0x4947: // gif
case 0x5089: // png
return true;
default:
return false;
}
}
/// <summary>
/// 以指定字符串作为分隔符将指定字符串分隔成数组.
/// </summary>
/// <param name="value">要分割的字符串.</param>
/// <param name="strSplit">字符串类型的分隔符.</param>
/// <param name="removeEmptyEntries">是否移除数据中元素为空字符串的项.</param>
/// <returns>分割后的数据</returns>
public static string[] Split(this string value, string strSplit, bool removeEmptyEntries = false)
{
return value.Split(new[] { strSplit }, removeEmptyEntries ? StringSplitOptions.RemoveEmptyEntries : StringSplitOptions.None);
}
/// <summary>
/// 支持汉字的字符串长度汉字长度计为2.
/// </summary>
/// <param name="value">参数字符串.</param>
/// <returns>当前字符串的长度汉字长度为2.</returns>
public static int TextLength(this string value)
{
ASCIIEncoding ascii = new ASCIIEncoding();
int tempLen = 0;
byte[] bytes = ascii.GetBytes(value);
foreach (byte b in bytes)
{
if (b == 63)
tempLen += 2;
else
tempLen += 1;
}
return tempLen;
}
/// <summary>
/// 将字符串转换为<see cref="byte"/>[]数组,默认编码为<see cref="Encoding.UTF8"/>.
/// </summary>
public static byte[] ToBytes(this string value, Encoding encoding = null)
{
if (encoding == null)
encoding = Encoding.UTF8;
return encoding.GetBytes(value);
}
/// <summary>
/// 将<see cref="byte"/>[]数组转换为字符串,默认编码为<see cref="Encoding.UTF8"/>.
/// </summary>
public static string ToString(this byte[] bytes, Encoding encoding)
{
if (encoding == null)
encoding = Encoding.UTF8;
return encoding.GetString(bytes);
}
#endregion
}