using System;
|
using System.Collections.Generic;
|
using System.Data.SqlTypes;
|
using System.Diagnostics.CodeAnalysis;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace FlexJobApi.Core
|
{
|
/// <summary>
|
/// 字符串工具
|
/// </summary>
|
public static class StringUtils
|
{
|
/// <summary>
|
/// 校验字符串为空
|
/// </summary>
|
/// <param name="str"></param>
|
/// <returns></returns>
|
public static bool IsNull([NotNullWhen(false)] this string str)
|
{
|
return string.IsNullOrWhiteSpace(str) || str == "undefined";
|
}
|
|
/// <summary>
|
/// 校验字段不为空
|
/// </summary>
|
/// <param name="str"></param>
|
/// <returns></returns>
|
public static bool IsNotNull([NotNullWhen(true)] this string str)
|
{
|
return !str.IsNull();
|
}
|
|
public static Guid? ToGuid(this string str)
|
{
|
if (str.IsNotNull() && Guid.TryParse(str, out var guid) && guid != Guid.Empty) return guid;
|
return null;
|
}
|
|
public static int? ToInt(this string str)
|
{
|
if (str.IsNotNull() && int.TryParse(str, out var @int)) return @int;
|
return null;
|
}
|
|
/// <summary>
|
/// 获取复数英文名
|
/// </summary>
|
/// <param name="singularName"></param>
|
/// <returns></returns>
|
public static string GetPluralizedName(this string singularName)
|
{
|
if (singularName.EndsWith("s") || singularName.EndsWith("x") || singularName.EndsWith("z") ||
|
singularName.EndsWith("ch") || singularName.EndsWith("sh"))
|
{
|
return singularName + "es";
|
}
|
else if (singularName.EndsWith("y") && !"aeiou".Contains(singularName[singularName.Length - 2]))
|
{
|
return singularName.Substring(0, singularName.Length - 1) + "ies";
|
}
|
else
|
{
|
return singularName + "s";
|
}
|
}
|
}
|
}
|