using System.Text.RegularExpressions;
namespace WcsMain.ExtendMethod;
///
/// 扩展方法,检查基础数据
///
public static partial class StringExtendMethod
{
///
/// 检测一个字符串是不是 NoRead
///
///
///
public static bool IsNoRead(this string? value)
{
if (string.IsNullOrEmpty(value)) return false;
return value.Replace(" ", "").Equals("noread", StringComparison.CurrentCultureIgnoreCase);
}
///
/// 检查一个字符串是否是数字
///
///
///
public static bool IsNumber(this string? value)
{
if (string.IsNullOrEmpty(value)) return false;
return IsNumberRegex().IsMatch(value);
}
///
/// 将字符串转换为 bool,只有等于null和0的情况下是false
///
///
///
public static bool ToBool(this string? value)
{
if(value == null) return false;
return value != "0";
}
///
/// 检测一个字符串是不是时间格式,如果是返回时间,如果不是返回 default
///
///
///
public static DateTime? ToDateTime(this string? value)
{
try
{
DateTime time = Convert.ToDateTime(value);
return time;
}
catch
{
return default;
}
}
///
/// 将载具号转换为 plc 可识别的载具号
///
///
///
public static int ToPlcVehicleNo(this string? value)
{
if (string.IsNullOrEmpty(value)) return 0;
try
{
return Convert.ToInt32(Regex.Replace(value, "\\D", ""));
}
catch
{
return 0;
}
}
[GeneratedRegex("^\\d+$")]
private static partial Regex IsNumberRegex();
}