using System; using System.Collections; using System.Collections.Generic; using System.Net; using XiaoZhiSharp.Kernels; namespace XiaoZhiSharp.Utils { public static class StringExtenstion { public static string RemoveStrings(this string destString, string removeString) { while (destString.Contains(removeString)) { destString = destString.Replace(removeString, ""); } return destString; } /// 指示指定的字符串是 null 还是 String.Empty 字符串 /// 字符串 /// public static Boolean IsNullOrEmpty(this String? value) => value == null || value.Length <= 0; /// 是否空或者空白字符串 /// 字符串 /// public static Boolean IsNullOrWhiteSpace(this String? value) { if (value != null) { for (var i = 0; i < value.Length; i++) { if (!Char.IsWhiteSpace(value[i])) return false; } } return true; } /// 把一个列表组合成为一个字符串,默认逗号分隔 /// /// 组合分隔符,默认逗号 /// public static String Join(this IEnumerable value, String separator = ",") { string result = string.Empty; if (value != null) { foreach (var item in value) { result += item + separator; } result = result.Remove(result.Length - separator.Length); } return result.ToString(); } /// 忽略大小写的字符串结束比较,判断是否以任意一个待比较字符串结束 /// 字符串 /// 待比较字符串数组 /// public static Boolean EndsWithIgnoreCase(this String? value, params String[] strs) { if (value == null || String.IsNullOrEmpty(value)) return false; foreach (var item in strs) { if (value.EndsWith(item, StringComparison.OrdinalIgnoreCase)) return true; } return false; } /// /// 将路径字符串转换为Linux和网页地址能够识别的路径字符串,以及Windows路径字符串。 /// 注:Windows系统是能够识别反斜杠'/'的。 /// /// /// /// public static string FormatPath(this string path, OperatingSystemType? operatingSystem = null) { string result = path?.Trim(); if (!string.IsNullOrWhiteSpace(result)) { if (operatingSystem.HasValue) { switch (operatingSystem) { case OperatingSystemType.Windows: result = result.Replace('/', '\\'); break; case OperatingSystemType.Linux: result = result.Replace('\\', '/'); break; default: result = result.Replace('\\', '/'); break; } } else { result = result.Replace('\\', '/'); } } return result; } /// /// 将字符串数组转换成字符串 /// public static string ArrayToString(this string[] strArray, string separator = "") { if (strArray == null) return string.Empty; return string.Join(separator, strArray); } /// /// 根据 数据类型转化常见类型,如果不成功,返回false /// /// /// /// /// public static bool GetTypeStringValue(this Type propertyType, object value, out string? objResult) { if (propertyType == typeof(bool)) objResult = value.ToString(); else if (propertyType == typeof(char)) objResult = value.ToString(); else if (propertyType == typeof(byte)) objResult = value.ToString(); else if (propertyType == typeof(sbyte)) objResult = value.ToString(); else if (propertyType == typeof(short)) objResult = value.ToString(); else if (propertyType == typeof(ushort)) objResult = value.ToString(); else if (propertyType == typeof(int)) objResult = value.ToString(); else if (propertyType == typeof(uint)) objResult = value.ToString(); else if (propertyType == typeof(long)) objResult = value.ToString(); else if (propertyType == typeof(ulong)) objResult = value.ToString(); else if (propertyType == typeof(float)) objResult = value.ToString(); else if (propertyType == typeof(double)) objResult = value.ToString(); else if (propertyType == typeof(decimal)) objResult = value.ToString(); else if (propertyType == typeof(DateTime)) objResult = value.ToString(); else if (propertyType == typeof(DateTimeOffset)) objResult = value.ToString(); else if (propertyType == typeof(string)) objResult = value.ToString(); else if (propertyType == typeof(IPAddress)) objResult = value.ToString(); else if (propertyType.IsEnum) objResult = value.ToString(); else { objResult = null; return false; } return true; } private static readonly char[] DotSeparator = new char[] { '.' }; private static readonly char[] SlashSeparator = new char[] { '/' }; private static readonly char[] CommaSeparator = new char[] { ',' }; private static readonly char[] SemicolonSeparator = new char[] { ';' }; private static readonly char[] Separator = new char[] { '-' }; /// /// 根据英文逗号分割字符串,去除空白的字符 /// public static string[]? SplitAndTrim(this string? str) { return str.Split(CommaSeparator, StringSplitOptions.RemoveEmptyEntries); } /// /// 根据-符号分割字符串,去除空白的字符 /// public static string[]? SplitByHyphen(this string? str) { return str.Split(Separator, StringSplitOptions.RemoveEmptyEntries); } /// /// 只按第一个匹配项分割字符串 /// /// 要分割的字符串 /// 分割字符 /// 包含分割结果的列表 public static List SplitFirst(this string str, char split) { List result = new List(); // 寻找第一个分割字符的位置 int index = str.IndexOf(split); if (index >= 0) { // 将第一个分割字符之前的部分添加到结果列表 result.Add(str.Substring(0, index).Trim()); // 将第一个分割字符之后的部分添加到结果列表 result.Add(str.Substring(index + 1).Trim()); } return result; } /// /// 根据英文小数点进行分割字符串,去除空白的字符 /// public static string[]? SplitStringByDelimiter(this string? str) { return str?.Split(DotSeparator, StringSplitOptions.RemoveEmptyEntries); } /// /// 根据斜杠进行分割字符串,去除空白的字符 /// public static string[]? SplitStringBySlash(this string? str) { return str?.Split(SlashSeparator, StringSplitOptions.RemoveEmptyEntries); } /// /// 根据英文分号分割字符串,去除空白的字符 /// public static string[]? SplitStringBySemicolon(this string? str) { return str.Split(SemicolonSeparator, StringSplitOptions.RemoveEmptyEntries); } /// /// 返回List,无其他处理 /// public static List StringToList(this string str) { return new List() { str }; } } }