using Alchemy.Core.Services;
using System.Text.RegularExpressions;
namespace Alchemy.Core.Extensions
{
public static class StringExtension
{
///
/// 字符串转Double
///
///
///
public static double ToDouble(this string? strInput)
{
if (strInput.IsNull())
return -1;
bool result = double.TryParse(strInput, out double number);
if (result)
return number;
else
return -1;
}
///
/// 字符串转Long
///
///
///
public static long ToLong(this string? strInput)
{
if (strInput.IsNull())
return -1;
bool result = long.TryParse(strInput, out long number);
if (result)
return number;
else
return -1;
}
///
/// 字符串转Int
///
///
///
public static int ToInt(this string? strInput)
{
if (strInput.IsNull())
return -1;
bool result = int.TryParse(strInput, out int number);
if (result)
return number;
else
return -1;
}
///
/// 字符串转Float
///
///
///
public static float ToFloat(this string? strInput)
{
if (strInput.IsNull())
return -1;
bool result = float.TryParse(strInput, out float number);
if (result)
return number;
else
return -1;
}
///
/// 字符串转布尔值
///
///
///
public static bool ToBool(this string? strInput)
{
if (strInput.IsNull())
return false;
return bool.Parse(strInput);
}
///
/// 字符串转字节
///
///
///
public static byte ToByte(this string strInput)
{
return Convert.ToByte(strInput);
}
///
/// 字符串转日期格式
///
///
///
public static DateTime ToDt(this string strDt)
{
strDt = strDt.Replace("-0", "/");
strDt = strDt.Replace("-", "/");
return DateTime.Parse(strDt);
}
///
/// 字符串日期转时间戳
///
///
///
public static long ToTimeStamp(this string strDateTime)
{
long timeStamp = 946656000;
try
{
timeStamp = (Convert.ToDateTime(strDateTime).Ticks - TimeZoneInfo.ConvertTimeFromUtc(new DateTime(1970, 1, 1), TimeZoneInfo.Local).Ticks) / 10000000;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return timeStamp;
}
///
/// 字符串是否为空
///
///
///
public static bool IsNull(this string? strParam)
{
return string.IsNullOrEmpty(strParam);
}
public static bool IsNotNull(this string? strParam)
{
return !string.IsNullOrEmpty(strParam);
}
///
/// 判断是否合规日期时间字符串
///
///
///
public static bool IsDateTime(this string strParam)
{
if (strParam.IsNull())
return false;
return RegexService.IsMatch(strParam, @"^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\s+(20|21|22|23|[0-1]\d):[0-5]\d:[0-5]\d$");
}
///
/// 判断是否合规日期字符串
///
///
///
public static bool IsDate(this string strParam)
{
if (strParam.IsNull())
return false;
return RegexService.IsMatch(strParam, @"^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))$");
}
///
/// 判断是否合规IP地址或域名
///
///
///
public static bool IsIPorDomain(this string strParam)
{
if (strParam.IsNull())
return false;
bool value = strParam.IsIPv4();
if (!value)
{
if (strParam.Contains("."))
{
value = true;
}
}
return value;
}
///
/// 检测是否IP地址
///
///
///
public static bool IsIPv4(this string strIP)
{
if (strIP.IsNull())
return false;
if (strIP.Length < 7 || strIP.Length > 15)
return false;
return RegexService.IsMatch(strIP, @"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$");
}
///
/// 判断是否版本号
///
///
///
public static bool IsVersion(this string strParam)
{
bool value = false;
if (strParam.Contains("."))
{
string[] arrVer = strParam.Split('.');
if (arrVer.Length > 1)
{
value = true;
foreach (string strVerNum in arrVer)
{
value = value & int.TryParse(strVerNum, out int verNum);
}
}
}
return value;
}
///
/// 判断是否包含中文
///
///
///
public static bool ContainsChinese(this string strParam)
{
return Regex.IsMatch(strParam, @"[\u4e00-\u9fa5]");
}
}
}