csharp/17MKH/Mkh/src/01_Utils/Utils/Extensions/StringExtensions.cs

StringExtensions.cs
using System;
using System.Linq;
using System.Text;

// ReSharper disable once CheckNamespace
namespace Mkh;

public static clast StringExtensions
{
    /// 
    /// 判断字符串是否为Null、空
    /// 
    /// 
    /// 
    public static bool IsNull(this string s)
    {
        return string.IsNullOrWhiteSpace(s);
    }

    /// 
    /// 判断字符串是否不为Null、空
    /// 
    /// 
    /// 
    public static bool NotNull(this string s)
    {
        return !string.IsNullOrWhiteSpace(s);
    }

    /// 
    /// 与字符串进行比较,忽略大小写
    /// 
    /// 
    /// 
    /// 
    public static bool EqualsIgnoreCase(this string s, string value)
    {
        return s.Equals(value, StringComparison.OrdinalIgnoreCase);
    }

    /// 
    /// 匹配字符串结尾,忽略大小写
    /// 
    /// 
    /// 
    /// 
    public static bool EndsWithIgnoreCase(this string s, string value)
    {
        return s.EndsWith(value, StringComparison.OrdinalIgnoreCase);
    }

    /// 
    /// 匹配字符串开头,忽略大小写
    /// 
    /// 
    /// 
    /// 
    public static bool StartsWithIgnoreCase(this string s, string value)
    {
        return s.StartsWith(value, StringComparison.OrdinalIgnoreCase);
    }

    /// 
    /// 首字母转小写
    /// 
    /// 
    /// 
    public static string FirstCharToLower(this string s)
    {
        if (string.IsNullOrEmpty(s))
            return s;

        string str = s.First().ToString().ToLower() + s.Substring(1);
        return str;
    }

    /// 
    /// 首字母转大写
    /// 
    /// 
    /// 
    public static string FirstCharToUpper(this string s)
    {
        if (string.IsNullOrEmpty(s))
            return s;

        string str = s.First().ToString().ToUpper() + s.Substring(1);
        return str;
    }

    /// 
    /// 转为Base64,UTF-8格式
    /// 
    /// 
    /// 
    public static string ToBase64(this string s)
    {
        return s.ToBase64(Encoding.UTF8);
    }

    /// 
    /// 转为Base64
    /// 
    /// 
    /// 编码
    /// 
    public static string ToBase64(this string s, Encoding encoding)
    {
        if (s.IsNull())
            return string.Empty;

        var bytes = encoding.GetBytes(s);
        return bytes.ToBase64();
    }

    /// 
    /// 转换为Base64
    /// 
    /// 
    /// 
    public static string ToBase64(this byte[] bytes)
    {
        if (bytes == null)
            return null;

        return Convert.ToBase64String(bytes);
    }

    /// 
    /// Base64解析
    /// 
    /// 
    /// 
    public static string FromBase64(this string s)
    {
        byte[] data = Convert.FromBase64String(s);
        return Encoding.UTF8.GetString(data);
    }
}