移动系统liao
2025-02-17 557c2711a3e103ebc3d0492344eca9730d5e92b2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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);
        }
    }
}