2025-10-11 18:25:59 +08:00

248 lines
9.3 KiB
C#
Raw Permalink 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;
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;
}
/// <summary>指示指定的字符串是 null 还是 String.Empty 字符串</summary>
/// <param name="value">字符串</param>
/// <returns></returns>
public static Boolean IsNullOrEmpty(this String? value) => value == null || value.Length <= 0;
/// <summary>是否空或者空白字符串</summary>
/// <param name="value">字符串</param>
/// <returns></returns>
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;
}
/// <summary>把一个列表组合成为一个字符串,默认逗号分隔</summary>
/// <param name="value"></param>
/// <param name="separator">组合分隔符,默认逗号</param>
/// <returns></returns>
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();
}
/// <summary>忽略大小写的字符串结束比较,判断是否以任意一个待比较字符串结束</summary>
/// <param name="value">字符串</param>
/// <param name="strs">待比较字符串数组</param>
/// <returns></returns>
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;
}
/// <summary>
/// 将路径字符串转换为Linux和网页地址能够识别的路径字符串,以及Windows路径字符串。
/// 注Windows系统是能够识别反斜杠'/'的。
/// </summary>
/// <param name="path"></param>
/// <param name="operatingSystem"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 将字符串数组转换成字符串
/// </summary>
public static string ArrayToString(this string[] strArray, string separator = "")
{
if (strArray == null)
return string.Empty;
return string.Join(separator, strArray);
}
/// <summary>
/// 根据<see cref="Type"/> 数据类型转化常见类型如果不成功返回false
/// </summary>
/// <param name="propertyType"></param>
/// <param name="value"></param>
/// <param name="objResult"></param>
/// <returns></returns>
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[] { '-' };
/// <summary>
/// 根据英文逗号分割字符串,去除空白的字符
/// </summary>
public static string[]? SplitAndTrim(this string? str)
{
return str.Split(CommaSeparator, StringSplitOptions.RemoveEmptyEntries);
}
/// <summary>
/// 根据-符号分割字符串,去除空白的字符
/// </summary>
public static string[]? SplitByHyphen(this string? str)
{
return str.Split(Separator, StringSplitOptions.RemoveEmptyEntries);
}
/// <summary>
/// 只按第一个匹配项分割字符串
/// </summary>
/// <param name="str">要分割的字符串</param>
/// <param name="split">分割字符</param>
/// <returns>包含分割结果的列表</returns>
public static List<string> SplitFirst(this string str, char split)
{
List<string> result = new List<string>();
// 寻找第一个分割字符的位置
int index = str.IndexOf(split);
if (index >= 0)
{
// 将第一个分割字符之前的部分添加到结果列表
result.Add(str.Substring(0, index).Trim());
// 将第一个分割字符之后的部分添加到结果列表
result.Add(str.Substring(index + 1).Trim());
}
return result;
}
/// <summary>
/// 根据英文小数点进行分割字符串,去除空白的字符
/// </summary>
public static string[]? SplitStringByDelimiter(this string? str)
{
return str?.Split(DotSeparator, StringSplitOptions.RemoveEmptyEntries);
}
/// <summary>
/// 根据斜杠进行分割字符串,去除空白的字符
/// </summary>
public static string[]? SplitStringBySlash(this string? str)
{
return str?.Split(SlashSeparator, StringSplitOptions.RemoveEmptyEntries);
}
/// <summary>
/// 根据英文分号分割字符串,去除空白的字符
/// </summary>
public static string[]? SplitStringBySemicolon(this string? str)
{
return str.Split(SemicolonSeparator, StringSplitOptions.RemoveEmptyEntries);
}
/// <summary>
/// 返回List,无其他处理
/// </summary>
public static List<string> StringToList(this string str)
{
return new List<string>() { str };
}
}
}