using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Business.Core.Utilities
{
public static class StringExtensions
{
///
/// 去掉字符串所有空格
///
/// 字符串
///
public static string TrimAll(this string? str)
{
return string.IsNullOrWhiteSpace(str) ? string.Empty : Regex.Replace(str, @"\s", "");
}
///
/// 去掉空格,如果为null或Empty则返回string.Empty
///
///
///
public static string TrimNullOrEmpty(this string? str)
{
return string.IsNullOrEmpty(str) ? string.Empty : str.Trim();
}
///
/// 转换为不为空的字符串
///
/// 字符串
/// 如果字符串为null返回string.Empty,否则返回字符串本身。
public static string ToEmptyString(this string? str)
{
return string.IsNullOrEmpty(str) ? string.Empty : str;
}
///
/// 将字符串分割为数组
///
/// 要分割的字符串
/// 分隔符
/// 分割选项
/// 字符串数组
public static string[] SplitToArray(this string? str, string separator = ",", StringSplitOptions options = StringSplitOptions.RemoveEmptyEntries)
{
return string.IsNullOrWhiteSpace(str) ? Array.Empty() : str.Split(separator, options);
}
///
/// 将字符串分割为long数组
///
/// 要分割的字符串
/// 分隔符
/// 分割选项
/// long List
public static List SplitToLongList(this string? str, string separator = ",", StringSplitOptions options = StringSplitOptions.RemoveEmptyEntries)
{
string[] array = str.SplitToArray(separator, options);
var list = new List();
foreach (string a in array)
{
if (a.IsLong(out long l))
{
list.Add(l);
}
}
return list;
}
///
/// 将字符串分割为int数组
///
/// 要分割的字符串
/// 分隔符
/// 分割选项
/// int List
public static List SplitToIntList(this string? str, string separator = ",", StringSplitOptions options = StringSplitOptions.RemoveEmptyEntries)
{
string[] array = str.SplitToArray(separator, options);
var list = new List();
foreach (string a in array)
{
if (a.IsInt(out int i))
{
list.Add(i);
}
}
return list;
}
///
/// 判断字符串是否是int类型数字
///
/// 字符串
///
public static bool IsInt(this string? str)
{
return int.TryParse(str, out _);
}
///
/// 判断字符串是否是int类型数字
///
/// 字符串
/// 如果是数字返回int
///
public static bool IsInt(this string? str, out int i)
{
return int.TryParse(str, out i);
}
///
/// 将字符串转换为int
///
/// 字符串
/// 转换失败时的默认值
///
public static int ToInt(this string? str, int defaultValue = 0)
{
return str.IsInt(out int i) ? i : defaultValue;
}
///
/// 判断字符串是否是long类型数字
///
/// 字符串
///
public static bool IsLong(this string? str)
{
return long.TryParse(str, out _);
}
///
/// 判断字符串是否是long类型数字
///
/// 字符串
/// 如果是数字返回long
///
public static bool IsLong(this string? str, out long l)
{
return long.TryParse(str, out l);
}
///
/// 将字符串转换为long
///
/// 字符串
/// 转换失败时的默认值
///
public static long ToLong(this string? str, long defaultValue = 0)
{
return str.IsLong(out long l) ? l : defaultValue;
}
///
/// 判断字符串是否是decimal类型数字
///
/// 字符串
///
public static bool IsDecimal(this string? str)
{
return decimal.TryParse(str, out _);
}
///
/// 判断字符串是否是decimal类型数字
///
/// 字符串
///
public static bool IsDecimal(this string? str, out decimal d)
{
return decimal.TryParse(str, out d);
}
///
/// 字符串转换为decimal类型数字
///
/// 字符串
/// 转换失败时的默认值
///
public static decimal ToDecimal(this string? str, decimal defaultValue = 0)
{
return str.IsDecimal(out decimal d) ? d : defaultValue;
}
///
/// 判断字符串是否是float类型数字
///
/// 字符串
///
public static bool IsFloat(this string? str, out float f)
{
return float.TryParse(str, out f);
}
///
/// 字符串转换为float类型数字
///
/// 字符串
/// 转换失败时的默认值
///
public static float ToFloat(this string? str, float defaultValue = 0)
{
return str.IsFloat(out float f) ? f : defaultValue;
}
///
/// 判断字符串是否是double类型数字
///
/// 字符串
///
public static bool IsDouble(this string? str, out double d)
{
return double.TryParse(str, out d);
}
///
/// 字符串转换为double类型数字
///
/// 字符串
/// 转换失败时的默认值
///
public static double ToDouble(this string? str, double defaultValue = 0)
{
return str.IsDouble(out double d) ? d : defaultValue;
}
///
/// 判断字符串是否为bool类型
///
/// 字符串
///
/// 如果字符串为"1"或"true"(不区分大小写,去掉所有空格)时返回true否则返回false
public static bool IsBool(this string? str)
{
if (string.IsNullOrEmpty(str))
{
return false;
}
string str1 = str.TrimAll();
return "true".EqualsIgnoreCase(str1) || "1".Equals(str1);
}
///
/// 将字符串转换为bool
///
/// 字符串
/// 如果字符串为1或true(不区分大小写),则返回true,否则返回false
public static bool ToBool(this string? str)
{
return str.IsBool();
}
///
/// 判断一个字符串是否是日期时间类型
///
/// 字符串
/// 输出转换后的日期时间
///
public static bool IsDateTime(this string? str, out DateTime dt)
{
return DateTime.TryParse(str, out dt);
}
///
/// 判断一个字符串是否是日期时间类型
///
/// 字符串
///
public static bool IsDateTime(this string? str)
{
return str.IsDateTime(out _);
}
///
/// 将字符串转换为时间时间
///
/// 要转换的字符串
/// 转换失败时返回的默认值
///
public static DateTime ToDateTime(this string? str, DateTime defaultValue)
{
return str.IsDateTime(out DateTime dt) ? dt : defaultValue;
}
///
/// 将字符串转换为时间时间
///
/// 要转换的字符串
/// 转换失败时返回的默认值
/// 如果转换失败则返回null
public static DateTime? ToDateTime(this string? str)
{
return str.IsDateTime(out DateTime dt) ? dt : new DateTime?();
}
///
/// 比较字符串区分大小写
///
/// 字符串
/// 要比较的字符串
/// 是否相同
public static bool EqualsCase(this string? str, string? str1)
{
return string.Equals(str, str1);
}
///
/// 比较字符串是否与参数中任意一个字符串相等
///
/// 字符串
/// 要比较的字符串
/// 是否与参数中任意一个字符串相等
public static bool EqualsAny(this string? str, params string[] strs)
{
if (string.IsNullOrEmpty(str))
{
return false;
}
foreach (string s in strs)
{
if (s.Equals(str))
{
return true;
}
}
return false;
}
///
/// 判断字符串与参数中的字符串都不相等
///
/// 字符串
/// 要比较的字符串
///
public static bool NotEqualsAny(this string? str, params string[] strs)
{
if (string.IsNullOrEmpty(str))
{
return false;
}
foreach (string s in strs)
{
if (s.Equals(str))
{
return false;
}
}
return true;
}
///
/// 比较字符串不区分大小写
///
/// 字符串
/// 要比较的字符串
/// 是否相同
public static bool EqualsIgnoreCase(this string? str, string? str1)
{
return string.Equals(str, str1, StringComparison.CurrentCultureIgnoreCase);
}
///
/// 比较字符串是否与参数中任意一个字符串相等(不区分大小写)
///
/// 字符串
/// 要比较的字符串
/// 是否与参数中任意一个字符串相等
public static bool EqualsAnyIgnoreCase(this string? str, params string[] strs)
{
if (string.IsNullOrEmpty(str))
{
return false;
}
foreach (string s in strs)
{
if (s.Equals(str, StringComparison.CurrentCultureIgnoreCase))
{
return true;
}
}
return false;
}
///
/// 判断字符串与参数中的字符串都不相等(不区分大小写)
///
/// 字符串
/// 要比较的字符串
/// 是否与参数中任意一个字符串相等
public static bool NotEqualsAnyIgnoreCase(this string? str, params string[] strs)
{
if (string.IsNullOrEmpty(str))
{
return false;
}
foreach (string s in strs)
{
if (s.Equals(str, StringComparison.CurrentCultureIgnoreCase))
{
return false;
}
}
return true;
}
///
/// 判断字符串中是否包含str1字符串(区分大小写)
///
/// 字符串
/// 被包含的字符串
/// StringComparison
///
public static bool ContainsCase(this string? str, string? str1)
{
return str != null && str1 != null && str.Contains(str1);
}
///
/// 判断字符串中是否包含str1字符串(不区分大小写)
///
/// 字符串
/// 被包含的字符串
///
public static bool ContainsIgnoreCase(this string? str, string? str1)
{
return str != null && str1 != null && str.Contains(str1, StringComparison.CurrentCultureIgnoreCase);
}
///
/// 判断字符串出现的位置(为区分大小写)
///
/// 字符串
/// 子字符串
///
public static int IndexOfIgnoreCase(this string? str, string str1)
{
return str == null ? -1 : str.IndexOf(str1, StringComparison.CurrentCultureIgnoreCase);
}
///
/// 去掉开头的字符串
///
/// 要去掉的字符串
/// 开头的字符串
///
public static string TrimStartString(this string? str, string trimString)
{
return string.IsNullOrWhiteSpace(str) ? string.Empty : !str.StartsWith(trimString) ? str : str.Substring(trimString.Length);
}
///
/// 去掉开头的字符串(不区分大小写)
///
/// 要去掉的字符串
/// 开头的字符串
///
public static string TrimStartStringIgnoreCase(this string? str, string trimString)
{
return string.IsNullOrWhiteSpace(str) ? string.Empty : !str.StartsWithIgnoreCase(trimString) ? str : str.Substring(trimString.Length);
}
///
/// 去掉结尾的字符串
///
/// 要去掉的字符串
/// 结尾的字符串
///
public static string TrimEndString(this string? str, string trimString)
{
return string.IsNullOrWhiteSpace(str) ? string.Empty : !str.EndsWith(trimString) ? str : str.Substring(0, str.Length - trimString.Length);
}
///
/// 去掉结尾的字符串(不区分大小写)
///
/// 要去掉的字符串
/// 结尾的字符串
///
public static string TrimEndStringIgnoreCase(this string? str, string trimString)
{
return string.IsNullOrWhiteSpace(str) ? string.Empty : !str.EndsWithIgnoreCase(trimString) ? str : str.Substring(0, str.Length - trimString.Length);
}
///
/// 是否是以字符串数组参数中任意一个字符串开头的字符串
///
/// 字符串
/// 开头的字符串数组
///
public static bool StartsWithAny(this string? str, params string[] values)
{
if (string.IsNullOrEmpty(str))
{
return false;
}
foreach (string value in values)
{
if (str.StartsWith(value))
{
return true;
}
}
return false;
}
///
/// 是否是以字符串数组参数中任意一个字符串开头的字符串
///
/// 字符串
/// StringComparison, 可设置是否区分大小写
/// 开头的字符串数组
///
public static bool StartsWithAny(this string? str, StringComparison stringComparison, params string[] values)
{
if (string.IsNullOrEmpty(str))
{
return false;
}
foreach (string value in values)
{
if (str.StartsWith(value, stringComparison))
{
return true;
}
}
return false;
}
///
/// 判断字符串是否以某个字符串开头(不区分大小写)
///
/// 要判断的字符串
/// 开头的字符串
///
public static bool StartsWithIgnoreCase(this string? str, string startString)
{
return !string.IsNullOrEmpty(str) && str.StartsWith(startString, StringComparison.CurrentCultureIgnoreCase);
}
///
/// 判断字符串是否以某个字符串结尾(不区分大小写)
///
/// 要判断的字符串
/// 结尾的字符串
///
public static bool EndsWithIgnoreCase(this string? str, string startString)
{
return !string.IsNullOrEmpty(str) && str.EndsWith(startString, StringComparison.CurrentCultureIgnoreCase);
}
///
/// 替换字符串,不区分大不写
///
/// 字符串
/// 要替换的字符串
/// 新字符串
///
public static string ReplaceIgnoreCase(this string? str, string oldValue, string? newValue)
{
return string.IsNullOrEmpty(str) ? string.Empty : str.Replace(oldValue, newValue, StringComparison.CurrentCultureIgnoreCase);
}
///
/// 将字符串转换为对应类型的对象
///
/// 字符串
///
public static object ToTypeObject(this string? str, out Type type)
{
if (string.IsNullOrEmpty(str) || str.EqualsIgnoreCase("null"))
{
type = typeof(string);
return string.Empty;
}
else if (str.IsInt(out int objInt))
{
type = typeof(int);
return objInt;
}
else if (str.IsLong(out long objLong))
{
type = typeof(long);
return objLong;
}
else if (Guid.TryParse(str, out Guid objGuid))
{
type = typeof(Guid);
return objGuid;
}
else if (str.IsFloat(out float objFloat))
{
type = typeof(float);
return objFloat;
}
else if (str.IsDouble(out double objDouble))
{
type = typeof(double);
return objDouble;
}
else if (str.IsDecimal(out decimal objDecimal))
{
type = typeof(decimal);
return objDecimal;
}
else if (str.IsDateTime(out DateTime objDateTime))
{
type = typeof(DateTime);
return objDateTime;
}
else if ("true".EqualsIgnoreCase(str) || "false".EqualsIgnoreCase(str))
{
type = typeof(bool);
return "true".EqualsIgnoreCase(str);
}
type = typeof(string);
return str;
}
///
/// url编码
///
/// 要编码的url
///
public static string UrlEncode(this string? url)
{
return string.IsNullOrWhiteSpace(url) ? string.Empty : Uri.EscapeDataString(url).Replace("'", "%27");
}
///
/// url解码
///
/// 要解码的url
///
public static string UrlDecode(this string? url)
{
return string.IsNullOrWhiteSpace(url) ? string.Empty : Uri.UnescapeDataString(url).Replace("%27", "'");
}
///
/// 检测对象是否为null,为null则抛出异常
///
/// 对象
/// 参数名
public static void CheckNull(this object obj, string parameterName)
{
if (obj == null)
throw new ArgumentNullException(parameterName);
}
///
/// 是否为空
///
/// 值
public static bool IsEmpty([NotNullWhen(false)] this string? value)
{
return string.IsNullOrWhiteSpace(value);
}
///
/// 是否为空
///
/// 值
public static bool IsEmpty(this Guid value)
{
return value == Guid.Empty;
}
///
/// 是否为空
///
/// 值
public static bool IsEmpty([NotNullWhen(false)] this Guid? value)
{
if (value == null)
return true;
return value == Guid.Empty;
}
///
/// 是否为空
///
/// 值
public static bool IsEmpty(this IEnumerable value)
{
if (value == null)
return true;
return !value.Any();
}
///
/// 安全转换为字符串,去除两端空格,当值为null时返回""
///
/// 输入值
public static string SafeString(this object input)
{
return input?.ToString()?.Trim() ?? string.Empty;
}
///
/// 安全获取值,当值为null时,不会抛出异常
///
/// 可空值
public static T SafeValue(this T? value) where T : struct
{
return value ?? default;
}
}
}