468 lines
11 KiB
C#
468 lines
11 KiB
C#
using System.Globalization;
|
||
using System.Text;
|
||
using System.Text.RegularExpressions;
|
||
|
||
namespace ShrlAlgoToolkit.RevitAddins.Common.Assists;
|
||
|
||
/// <summary>
|
||
/// 各种验证帮助类
|
||
/// </summary>
|
||
public class ValidatorAssist
|
||
{
|
||
/// <summary>
|
||
/// 截取指定长度字符串
|
||
/// </summary>
|
||
/// <param name="inputString">要处理的字符串</param>
|
||
/// <param name="len">指定长度</param>
|
||
/// <returns>返回处理后的字符串</returns>
|
||
public static string ClipString(string inputString, int len)
|
||
{
|
||
var isShowFix = false;
|
||
if (len % 2 == 1)
|
||
{
|
||
isShowFix = true;
|
||
len--;
|
||
}
|
||
|
||
var ascii = new ASCIIEncoding();
|
||
var tempLen = 0;
|
||
var tempString = string.Empty;
|
||
var s = ascii.GetBytes(inputString);
|
||
for (var i = 0; i < s.Length; i++)
|
||
{
|
||
if (s[i] == 63)
|
||
{
|
||
tempLen += 2;
|
||
}
|
||
else
|
||
{
|
||
tempLen += 1;
|
||
}
|
||
|
||
try
|
||
{
|
||
tempString += inputString.Substring(i, 1);
|
||
}
|
||
catch
|
||
{
|
||
break;
|
||
}
|
||
|
||
if (tempLen > len)
|
||
{
|
||
break;
|
||
}
|
||
}
|
||
|
||
var mybyte = Encoding.Default.GetBytes(inputString);
|
||
if (isShowFix && mybyte.Length > len)
|
||
{
|
||
tempString += "…";
|
||
}
|
||
|
||
return tempString;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获得两个日期的间隔
|
||
/// </summary>
|
||
/// <param name="dateTime1">日期一。</param>
|
||
/// <param name="dateTime2">日期二。</param>
|
||
/// <returns>日期间隔TimeSpan。</returns>
|
||
public static TimeSpan DateDiff(DateTime dateTime1, DateTime dateTime2)
|
||
{
|
||
var ts1 = new TimeSpan(dateTime1.Ticks);
|
||
var ts2 = new TimeSpan(dateTime2.Ticks);
|
||
var ts = ts1.Subtract(ts2).Duration();
|
||
return ts;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 格式化日期时间
|
||
/// </summary>
|
||
/// <param name="dateTime1">日期时间</param>
|
||
/// <param name="dateMode">显示模式</param>
|
||
/// <returns>0-9种模式的日期</returns>
|
||
public static string FormatDate(DateTime dateTime1, string dateMode)
|
||
{
|
||
return dateMode switch
|
||
{
|
||
"0" => dateTime1.ToString("yyyy-MM-dd"),
|
||
"1" => dateTime1.ToString("yyyy-MM-dd HH:mm:ss"),
|
||
"2" => dateTime1.ToString("yyyy/MM/dd"),
|
||
"3" => dateTime1.ToString("yyyy年MM月dd日"),
|
||
"4" => dateTime1.ToString("MM-dd"),
|
||
"5" => dateTime1.ToString("MM/dd"),
|
||
"6" => dateTime1.ToString("MM月dd日"),
|
||
"7" => dateTime1.ToString("yyyy-MM"),
|
||
"8" => dateTime1.ToString("yyyy/MM"),
|
||
"9" => dateTime1.ToString("yyyy年MM月"),
|
||
_ => dateTime1.ToString(CultureInfo.InvariantCulture),
|
||
};
|
||
}
|
||
|
||
/// <summary>
|
||
/// 得到随机日期
|
||
/// </summary>
|
||
/// <param name="time1">起始日期</param>
|
||
/// <param name="time2">结束日期</param>
|
||
/// <returns>间隔日期之间的 随机日期</returns>
|
||
public static DateTime GetRandomTime(DateTime time1, DateTime time2)
|
||
{
|
||
var random = new Random();
|
||
DateTime minTime;
|
||
|
||
var ts = new TimeSpan(time1.Ticks - time2.Ticks);
|
||
|
||
// 获取两个时间相隔的秒数
|
||
var dTotalSecontds = ts.TotalSeconds;
|
||
int iTotalSecontds;
|
||
|
||
if (dTotalSecontds > int.MaxValue)
|
||
{
|
||
iTotalSecontds = int.MaxValue;
|
||
}
|
||
else
|
||
{
|
||
iTotalSecontds = dTotalSecontds < int.MinValue ? int.MinValue : (int)dTotalSecontds;
|
||
}
|
||
|
||
if (iTotalSecontds > 0)
|
||
{
|
||
minTime = time2;
|
||
}
|
||
else if (iTotalSecontds < 0)
|
||
{
|
||
minTime = time1;
|
||
}
|
||
else
|
||
{
|
||
return time1;
|
||
}
|
||
|
||
var maxValue = iTotalSecontds;
|
||
|
||
if (iTotalSecontds <= int.MinValue)
|
||
{
|
||
maxValue = int.MinValue + 1;
|
||
}
|
||
|
||
var i = random.Next(Math.Abs(maxValue));
|
||
|
||
return minTime.AddSeconds(i);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 验证是否包含汉语
|
||
/// </summary>
|
||
/// <param name="source">The source<see cref="string" /></param>
|
||
/// <returns>The <see cref="bool" /></returns>
|
||
public static bool HasChinese(string source)
|
||
{
|
||
return Regex.IsMatch(source, @"[\u4e00-\u9fa5]+", RegexOptions.IgnoreCase);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 全部是中文
|
||
/// </summary>
|
||
/// <param name="source"></param>
|
||
/// <returns></returns>
|
||
public static bool IsChinese(string source)
|
||
{
|
||
return Regex.IsMatch(source, @"^[\u4e00-\u9fa5]+$", RegexOptions.IgnoreCase);
|
||
}
|
||
|
||
/// <summary>
|
||
/// The IsDate
|
||
/// </summary>
|
||
/// <param name="str">The str<see cref="string" /></param>
|
||
/// <returns>The <see cref="bool" /></returns>
|
||
public static bool IsDate(string str)
|
||
{
|
||
//考虑到了4年一度的366天,还有特殊的2月的日期
|
||
var reg = new Regex(
|
||
@"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\d):[0-5]?\d:[0-5]?\d$"
|
||
);
|
||
return reg.IsMatch(str);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 是否为数字型
|
||
/// </summary>
|
||
/// <param name="strNumber"></param>
|
||
/// <returns></returns>
|
||
public static bool IsDecimal(string strNumber)
|
||
{
|
||
return new Regex(@"^([0-9])[0-9]*(\.\w*)?$").IsMatch(strNumber);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 验证是否为整数 如果为空,认为验证不合格 返回false
|
||
/// </summary>
|
||
/// <param name="number">要验证的整数</param>
|
||
/// <returns>The <see cref="bool" /></returns>
|
||
public static bool IsInt(string number)
|
||
{
|
||
//如果为空,认为验证不合格
|
||
if (IsNullOrEmpty(number))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
//清除要验证字符串中的空格
|
||
number = number.Trim();
|
||
|
||
//模式字符串
|
||
var pattern = @"^[0-9]+[0-9]*$";
|
||
|
||
//验证
|
||
return Regex.IsMatch(number, pattern);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 是不是Int型的
|
||
/// </summary>
|
||
/// <param name="source"></param>
|
||
/// <returns></returns>
|
||
public static bool IsIntInRegex(string source)
|
||
{
|
||
var regex = new Regex(@"^(-){0,1}\d+$");
|
||
if (regex.Match(source).Success)
|
||
{
|
||
return long.Parse(source) is <= 0x7fffffffL and >= (-2147483648L);
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 看字符串的长度是不是在限定数之间 一个中文为两个字符
|
||
/// </summary>
|
||
/// <param name="source">字符串</param>
|
||
/// <param name="begin">大于等于</param>
|
||
/// <param name="end">小于等于</param>
|
||
/// <returns></returns>
|
||
public static bool IsLengthStr(string source, int begin, int end)
|
||
{
|
||
var length = Regex.Replace(source, @"[^\x00-\xff]", "OK").Length;
|
||
return length > begin || length < end;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 验证是不是正常字符 字母,数字,下划线的组合
|
||
/// </summary>
|
||
/// <param name="source"></param>
|
||
/// <returns></returns>
|
||
public static bool IsNormalChar(string source)
|
||
{
|
||
return Regex.IsMatch(source, @"[\w\d_]+", RegexOptions.IgnoreCase);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 判断对象是否为空,为空返回true
|
||
/// </summary>
|
||
/// <typeparam name="T">要验证的对象的类型</typeparam>
|
||
/// <param name="data">要验证的对象</param>
|
||
/// <returns>The <see cref="bool" /></returns>
|
||
public static bool IsNullOrEmpty<T>(T data)
|
||
{
|
||
//如果为null
|
||
if (data == null)
|
||
{
|
||
return true;
|
||
}
|
||
|
||
//如果为""
|
||
if (data is string)
|
||
{
|
||
if (string.IsNullOrEmpty(data.ToString().Trim()))
|
||
{
|
||
return true;
|
||
}
|
||
}
|
||
|
||
//如果为DBNull
|
||
if (data is DBNull)
|
||
{
|
||
return true;
|
||
}
|
||
|
||
//不为空
|
||
return false;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 判断对象是否为空,为空返回true
|
||
/// </summary>
|
||
/// <param name="data">要验证的对象</param>
|
||
/// <returns>The <see cref="bool" /></returns>
|
||
public static bool IsNullOrEmpty(object data)
|
||
{
|
||
//如果为null
|
||
if (data == null)
|
||
{
|
||
return true;
|
||
}
|
||
|
||
//如果为""
|
||
if (data is string)
|
||
{
|
||
if (string.IsNullOrEmpty(data.ToString().Trim()))
|
||
{
|
||
return true;
|
||
}
|
||
}
|
||
|
||
//如果为DBNull
|
||
if (data is DBNull)
|
||
{
|
||
return true;
|
||
}
|
||
|
||
//不为空
|
||
return false;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 判断字符串是否为数字
|
||
/// </summary>
|
||
/// <param name="str">待验证的字符窜</param>
|
||
/// <returns>bool</returns>
|
||
public static bool IsNumber(string str)
|
||
{
|
||
return str.All(ar => char.IsNumber(ar));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 验证是否为数字
|
||
/// </summary>
|
||
/// <param name="number">要验证的数字</param>
|
||
/// <returns>The <see cref="bool" /></returns>
|
||
public static bool IsNumberInRegex(string number)
|
||
{
|
||
//如果为空,认为验证不合格
|
||
if (IsNullOrEmpty(number))
|
||
{
|
||
return false;
|
||
}
|
||
|
||
//清除要验证字符串中的空格
|
||
number = number.Trim();
|
||
|
||
//模式字符串
|
||
var pattern = @"^[0-9]+[0-9]*[.]?[0-9]*$";
|
||
|
||
//验证
|
||
return Regex.IsMatch(number, pattern);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 检测客户输入的字符串是否有效,并将原始字符串修改为有效字符串或空字符串。
|
||
/// 当检测到客户的输入中有攻击性危险字符串,则返回false,有效返回true。
|
||
/// </summary>
|
||
/// <param name="input">要检测的字符串</param>
|
||
/// <returns>The <see cref="bool" /></returns>
|
||
public static bool IsValidInput(ref string input)
|
||
{
|
||
try
|
||
{
|
||
if (IsNullOrEmpty(input))
|
||
{
|
||
//如果是空值,则跳出
|
||
return true;
|
||
}
|
||
|
||
//替换单引号
|
||
input = input.Replace("'", "''").Trim();
|
||
|
||
//检测攻击性危险字符串
|
||
var testString = "and |or |exec |insert |select |delete |update |count |chr |mid |master |truncate |char |declare ";
|
||
var testArray = testString.Split('|');
|
||
foreach (var testStr in testArray)
|
||
{
|
||
if (input.ToLower().IndexOf(testStr, StringComparison.Ordinal) != -1)
|
||
{
|
||
//检测到攻击字符串,清空传入的值
|
||
input = string.Empty;
|
||
return false;
|
||
}
|
||
}
|
||
|
||
//未检测到攻击字符串
|
||
return true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw new Exception(ex.Message);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 得到字符串长度,一个汉字长度为2
|
||
/// </summary>
|
||
/// <param name="inputString">参数字符串</param>
|
||
/// <returns></returns>
|
||
public static int StrLength(string inputString)
|
||
{
|
||
var ascii = new ASCIIEncoding();
|
||
var tempLen = 0;
|
||
var s = ascii.GetBytes(inputString);
|
||
for (var i = 0; i < s.Length; i++)
|
||
{
|
||
if (s[i] == 63)
|
||
{
|
||
tempLen += 2;
|
||
}
|
||
else
|
||
{
|
||
tempLen += 1;
|
||
}
|
||
}
|
||
|
||
return tempLen;
|
||
}
|
||
|
||
/// <summary>
|
||
/// The IsValidByte
|
||
/// </summary>
|
||
/// <param name="strIn">The strIn<see cref="string" /></param>
|
||
/// <returns>The <see cref="bool" /></returns>
|
||
internal bool IsValidByte(string strIn)
|
||
{
|
||
return Regex.IsMatch(strIn, @"^[a-z]{4,12}$");
|
||
}
|
||
|
||
/// <summary>
|
||
/// The IsValidDate
|
||
/// </summary>
|
||
/// <param name="strIn">The strIn<see cref="string" /></param>
|
||
/// <returns>The <see cref="bool" /></returns>
|
||
internal bool IsValidDate(string strIn)
|
||
{
|
||
return Regex.IsMatch(
|
||
strIn,
|
||
@"^2d{3}-(?:0?[1-9]|1[0-2])-(?:0?[1-9]|[1-2]d|3[0-1])(?:0?[1-9]|1d|2[0-3]):(?:0?[1-9]|[1-5]d):(?:0?[1-9]|[1-5]d)$"
|
||
);
|
||
}
|
||
|
||
/// <summary>
|
||
/// The IsValidDecimal
|
||
/// </summary>
|
||
/// <param name="strIn">The strIn<see cref="string" /></param>
|
||
/// <returns>The <see cref="bool" /></returns>
|
||
internal bool IsValidDecimal(string strIn)
|
||
{
|
||
return Regex.IsMatch(strIn, @"[0].d{1,2}|[1]");
|
||
}
|
||
|
||
/// <summary>
|
||
/// The IsValidPostfix
|
||
/// </summary>
|
||
/// <param name="strIn">The strIn<see cref="string" /></param>
|
||
/// <returns>The <see cref="bool" /></returns>
|
||
internal bool IsValidPostfix(string strIn)
|
||
{
|
||
return Regex.IsMatch(strIn, @".(?i:gif|jpg)$");
|
||
}
|
||
}
|