wms_serve_m_jingwangchengpin/WmsMobileServe/Utils/StringUtils.cs

55 lines
1.3 KiB
C#
Raw Normal View History

2025-01-08 15:43:26 +08:00
using System.Text.RegularExpressions;
namespace WmsMobileServe.Utils;
public static class StringUtils
{
/// <summary>
/// 判断一个字符串是不是 decimal
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool IsDecimal(this string? value)
{
if(string.IsNullOrWhiteSpace(value)) return false;
return decimal.TryParse(value, out _);
2025-01-08 15:43:26 +08:00
}
/// <summary>
/// 判断一个字符串 是不是 decimal
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool IsNotDecimal(this string? value)
{
return !value.IsDecimal();
}
/// <summary>
/// 将字符串转换为 decimal
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static decimal? ToDecimal(this string? value)
{
try
{
if (!value.IsDecimal()) return default;
return Convert.ToDecimal(value);
}
catch { return default; }
}
public static bool IsNumber(this string? value)
{
if (string.IsNullOrWhiteSpace(value)) return false;
return Regex.IsMatch(value, "^\\d+$");
}
public static bool IsNotNumber(this string? value)
{
return !IsNumber(value);
}
2025-01-08 15:43:26 +08:00
}