sunpengfei
2025-08-14 b495fa9f03462cf6007e1e035a7ed8abe5382c1c
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using Furion.DataValidation;
using Furion.FriendlyException;
using Mapster.Utils;
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace FlexJobApi.Core
{
    /// <summary>
    /// 字符串工具
    /// </summary>
    public static class StringUtils
    {
        /// <summary>
        /// 校验字符串为空
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNull([NotNullWhen(false)] this string str)
        {
            return string.IsNullOrWhiteSpace(str) || str == "undefined";
        }
 
        /// <summary>
        /// 校验字段不为空
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNotNull([NotNullWhen(true)] this string str)
        {
            return !str.IsNull();
        }
 
        public static Guid? ToGuid(this string str, string requiredMessage = null)
        {
            if (str.IsNotNull() && Guid.TryParse(str, out var guid) && guid != Guid.Empty) return guid;
            if (requiredMessage.IsNotNull()) throw Oops.Oh(EnumErrorCodeType.s400, requiredMessage);
            return null;
        }
 
        public static int? ToInt(this string str)
        {
            if (str.IsNotNull() && int.TryParse(str, out var @int)) return @int;
            return null;
        }
 
        public static T? ToEnum<T>(this string str, string requiredMessage = null)
             where T : struct
        {
            if (str.IsNotNull() && Enum.TryParse<T>(str, out var @enum)) return @enum;
            if (requiredMessage.IsNotNull()) throw Oops.Oh(EnumErrorCodeType.s400, requiredMessage);
            return null;
        }
 
        /// <summary>
        /// 获取复数英文名
        /// </summary>
        /// <param name="singularName"></param>
        /// <returns></returns>
        public static string GetPluralizedName(this string singularName)
        {
            if (singularName.EndsWith("s") || singularName.EndsWith("x") || singularName.EndsWith("z") ||
                singularName.EndsWith("ch") || singularName.EndsWith("sh"))
            {
                return singularName + "es";
            }
            else if (singularName.EndsWith("y") && !"aeiou".Contains(singularName[singularName.Length - 2]))
            {
                return singularName.Substring(0, singularName.Length - 1) + "ies";
            }
            else
            {
                return singularName + "s";
            }
        }
 
        public static bool CheckIsIdentityNumber18(this string identity)
        {
            return identity.IsNotNull() && identity.TryValidate(EnumValidationTypes.Identity).IsValid && identity.Length == 18;
        }
 
        /// <summary>
        /// 获取性别
        /// </summary>
        /// <param name="identity"></param>
        /// <returns></returns>
        public static EnumUserGender? GetGender(this string identity)
        {
            if (identity.CheckIsIdentityNumber18())
            {
                return identity[16] % 2 == 0
                    ? EnumUserGender.Female
                    : EnumUserGender.Male;
            }
            else
            {
                return null;
            }
        }
 
        /// <summary>
        /// 获取生日
        /// </summary>
        /// <param name="identity"></param>
        /// <returns></returns>
        public static DateTime? GetBirthday(this string identity)
        {
            if (identity.CheckIsIdentityNumber18())
            {
                return new DateTime(
                    identity.Substring(6, 4).ToInt()!.Value,
                    identity.Substring(10, 2).ToInt()!.Value,
                    identity.Substring(12, 2).ToInt()!.Value);
            }
            else
            {
                return null;
            }
        }
 
        /// <summary>
        /// 获取年龄
        /// </summary>
        /// <param name="identity"></param>
        /// <returns></returns>
        public static int? GetAge(this string identity)
        {
            if (identity.CheckIsIdentityNumber18())
            {
                var birthday = identity.GetBirthday();
                return birthday.GetAge();
            }
            else
            {
                return null;
            }
        }
 
    }
}