using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Text.RegularExpressions;
|
using System.Threading.Tasks;
|
|
namespace CoreCms.Net.Utility.Extensions
|
{
|
/// <summary>
|
/// 字符串扩展
|
/// </summary>
|
public static class StringExtensions
|
{
|
/// <summary>
|
/// str 转int
|
/// </summary>
|
/// <param name="value"></param>
|
/// <param name="defaultValue"></param>
|
/// <returns></returns>
|
public static int ToInt32OrDefault(this string value, int defaultValue = 0)
|
{
|
if (int.TryParse(value, out int result))
|
{
|
return result;
|
}
|
return defaultValue;
|
}
|
|
/// <summary>
|
/// 将yyyy-MM-dd'T'HH:mm:ss~yyyy-MM-dd'T'HH:mm:ss 格式未StartDate 和enddata
|
/// </summary>
|
/// <param name="input"></param>
|
/// <returns></returns>
|
public static (DateTime? StartDate, DateTime? EndDate) ParseDateTimeRange(this string input)
|
{
|
// 定义日期时间格式
|
string format = "yyyy-MM-dd'T'HH:mm:ss";
|
|
// 拆分字符串
|
string[] parts = input.Split('~');
|
|
if (parts.Length == 2)
|
{
|
// 解析开始时间
|
DateTime startDate;
|
bool isStartDateValid = DateTime.TryParseExact(parts[0], format, null, System.Globalization.DateTimeStyles.None, out startDate);
|
|
// 解析结束时间
|
DateTime endDate;
|
bool isEndDateValid = DateTime.TryParseExact(parts[1], format, null, System.Globalization.DateTimeStyles.None, out endDate);
|
|
if (isStartDateValid && isEndDateValid)
|
{
|
if(startDate<endDate)
|
return (startDate, endDate);
|
else
|
{
|
return (null, null);
|
}
|
}
|
else if (isStartDateValid)
|
{
|
return (startDate, null);
|
}
|
else if (isEndDateValid)
|
{
|
return (null, endDate);
|
}
|
}
|
|
// 如果输入字符串格式不正确或解析失败,返回 (null, null)
|
return (null, null);
|
}
|
/// <summary>
|
/// 校验是否是手机号码
|
/// </summary>
|
/// <param name="phoneNumber"></param>
|
/// <returns></returns>
|
public static bool IsPhoneNumberValid(this string phoneNumber)
|
{
|
// 定义正则表达式模式
|
string pattern = @"^1[3-9]\d{9}$";
|
|
// 使用 Regex.IsMatch 方法进行匹配
|
return Regex.IsMatch(phoneNumber, pattern);
|
}
|
}
|
}
|