You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

117 lines
3.5 KiB

using System;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using Tiobon.Core.Common.DB.Dapper.Const;
using Tiobon.Core.Common.DB.Dapper.Enums;
namespace Tiobon.Core.Common.DB.Dapper.Extensions
{
public static class StringExtension
{
public static bool _windows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
public static string ReplacePath(this string path)
{
if (string.IsNullOrEmpty(path))
return "";
if (_windows)
return path.Replace("/", "\\");
return path.Replace("\\", "/");
}
public static bool GetGuid(this string guid, out Guid outId)
{
Guid emptyId = Guid.Empty;
return Guid.TryParse(guid, out outId);
}
public static bool IsGuid(this string guid)
{
Guid newId;
return guid.GetGuid(out newId);
}
public static bool IsInt(this object obj)
{
if (obj == null)
return false;
bool reslut = Int32.TryParse(obj.ToString(), out int _number);
return reslut;
}
public static bool IsDate(this object str)
{
return str.IsDate(out _);
}
public static bool IsDate(this object str, out DateTime dateTime)
{
dateTime = DateTime.Now;
if (str == null || str.ToString() == "")
{
return false;
}
return DateTime.TryParse(str.ToString(), out dateTime);
}
/// <summary>
/// 根据传入格式判断是否为小数
/// </summary>
/// <param name="str"></param>
/// <param name="formatString">18,5</param>
/// <returns></returns>
public static bool IsNumber(this string str, string formatString)
{
if (string.IsNullOrEmpty(str))
return false;
int precision = 32;
int scale = 5;
try
{
if (string.IsNullOrEmpty(formatString))
{
precision = 10;
scale = 2;
}
else
{
string[] numbers = formatString.Split(',');
precision = Convert.ToInt32(numbers[0]);
scale = numbers.Length == 0 ? 2 : Convert.ToInt32(numbers[1]);
}
}
catch { };
return IsNumber(str, precision, scale);
}
/**/
/// <summary>
/// 判断一个字符串是否为合法数字(指定整数位数和小数位数)
/// </summary>
/// <param name="str">字符串</param>
/// <param name="precision">整数位数</param>
/// <param name="scale">小数位数</param>
/// <returns></returns>
public static bool IsNumber(this string str, int precision, int scale)
{
if ((precision == 0) && (scale == 0))
{
return false;
}
string pattern = @"(^\d{1," + precision + "}";
if (scale > 0)
{
pattern += @"\.\d{0," + scale + "}$)|" + pattern;
}
pattern += "$)";
return Regex.IsMatch(str, pattern);
}
public static int GetInt(this object obj)
{
if (obj == null)
return 0;
int.TryParse(obj.ToString(), out int _number);
return _number;
}
}
}