Falcon.SugarApi/Falcon.SugarApi/StringExtend.cs
2022-05-25 09:19:38 +08:00

42 lines
1.8 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;
using System.Diagnostics.CodeAnalysis;
namespace Falcon.SugarApi
{
/// <summary>
/// 字符串扩展
/// </summary>
public static class StringExtend
{
/// <summary>
/// 字符串是否为空或null
/// </summary>
/// <param name="str">字符串</param>
/// <returns>空或null为True否则False</returns>
public static bool IsNullOrEmpty(this string? str) => str == null || string.IsNullOrEmpty(str);
/// <summary>
/// 字符串是否不为空或null
/// </summary>
/// <param name="str">字符串</param>
/// <returns>与IsNullOrEmpty相反</returns>
public static bool IsNotNullOrEmpty(this string? str) => !str.IsNullOrEmpty();
/// <summary>
/// 当字符串IsNullOrEmpty为True的时候返回默认值否则返回本身
/// </summary>
/// <param name="str">字符串</param>
/// <param name="defaultVal">默认值</param>
/// <returns>字符串本身或默认值</returns>
public static string? ToDefault(this string? str, [NotNull] string defaultVal) => str.IsNullOrEmpty() ? defaultVal : str;
/// <summary>
/// 使用全角半角的逗号句号和分号分割字符串。
/// </summary>
/// <param name="str">要分割的字符串</param>
/// <param name="splitChars"></param>
/// <returns>字符串数组。当str为null时返回空数组</returns>
public static string[] SplitStr(this string? str, params char[] splitChars)
=> str.IsNullOrEmpty() ? Array.Empty<string>() : str.Split(splitChars.Length == 0 ? new char[] { ',', '', ';', '', '.', '。' } : splitChars, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
}
}