移动系统liao
2025-03-19 16b063870156db78148440971ab46c649e3e6018
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
 
namespace cylsg.utility.Extend
{
    /// <summary>
    /// 字符串扩展
    /// </summary>
    public static class StringEx
    {
        /// <summary>
        /// 首字母小写
        /// </summary>
        /// <param name="self"></param>
        /// <returns></returns>
        public static string FirstToLower(this string self)
        {
            string str1 = self?.Substring(0, 1).ToLower() ?? "";
            string str2 = self?.Substring(1) ?? "";
 
            //首字母转小写
            return str1 + str2;
        }
        /// <summary>
        /// 带有xxx.xxx结束的的所有字符串 的后面xxx.xxx
        /// </summary>
        /// <param name="strtypr"></param>
        /// <returns></returns>
        public static string GetFileName(this string strtypr)
        {
            return Path.GetFileName(strtypr);
        }
        /// <summary>
        /// 是否是数字序列
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool isPureNum(this string strtypr)
        {
            if (strtypr.Length == 0 || strtypr == null)//验证这个字符串是否为空
            {
                return false;
            }
            byte[] strBytes = Encoding.ASCII.GetBytes(strtypr);//获取字符串的byte类型的字符数组,编码方式ASCII
            foreach (byte strByte in strBytes)
            {
                if (strByte < 48 || strByte > 57)     //判断每个字符是否为数字,根据每个字符的ASCII值所在范围判断
                {
                    return false;                     //不是,就返回false
                }
            }
            return true;                              //是,就返回true
        }
 
        /// <summary>
        /// 是否是数字序列
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool isGuid(this string strtypr)
        {
            if (strtypr.Length < 20 || strtypr == null)//验证这个字符串是否为空
            {
                return false;
            }
            try
            {
                new Guid(strtypr);
                return true;
 
            }
            catch (Exception)
            {
 
                return false;
            }
        }
 
        /// <summary>
        /// 字符串
        /// </summary>
        /// <param name="strtypr"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static int toInt(this string strtypr)
        {
            if (!strtypr.isPureNum())
                throw new Exception("并非完整的数字序列");
            return int.Parse(strtypr);
        }
 
        /// <summary>
        /// 移除第一个字符串为指定字符串的 如 “axtXXXXX" 如果以"axt"开头的字符串,就移除"axt" 没有则不变
        /// </summary>
        /// <param name="strtypr"></param>
        /// <param name="str">开始字符</param>
        /// <returns></returns>
        public static string RemoveStartWithStr(this string strtypr, string str)
        {
            string strret = strtypr;
            if (strtypr.StartsWith(str))
            {
                strret = strtypr.Remove(0, str.Length);
            }
            return strret;
 
        }
 
        /// <summary>
        /// 对字符串进行隐私处理,比如中间的用*显示
        /// </summary>
        /// <param name="strtypr"></param>
        /// <returns></returns>
        public static string PrivacyStr(this string strtypr)
        {
            // 如果是手机号码,仅保留前3位和后4位,中间用星号替换
            if (Regex.IsMatch(strtypr, @"^1[3-9]\d{9}$"))
            {
                return $"{strtypr.Substring(0, 3)}****{strtypr.Substring(7)}";
            }
 
            // 对于中文名字或者非手机号格式,仅保留首尾各1个字符,中间用星号替换
            else
            {
                if (strtypr.Length <= 2) return $"{strtypr.Substring(0, 1)}*"; // 长度小于等于2时,返回固定的星号
 
                var masked = $"{strtypr.Substring(0, 1)}*{strtypr.Substring(strtypr.Length - 1, 1)}";
                return masked;
            }
        }
 
 
        #region 时间区间字符串转其实和结束时间
        /// <summary>
        /// 解析时间区间字符串
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static (DateTime? StartDate, DateTime? EndDate) TryParseDateRangString(this string input)
        {
            DateTime startDate, endDate;
            var culture = CultureInfo.InvariantCulture;
 
            // 尝试解析第一种格式 "2023-01-012023-01-31"
            if (DateTime.TryParseExact(input.Substring(0, 10), "yyyy-MM-dd", culture, DateTimeStyles.None, out startDate) &&
                DateTime.TryParseExact(input.Substring(10), "yyyy-MM-dd", culture, DateTimeStyles.None, out endDate))
            {
                return (startDate, endDate);
            }
 
            // 尝试解析第二种格式 "2023-01-01 08:02:032023-01-31 08:02:03"
            if (DateTime.TryParseExact(input.Substring(0, 19), "yyyy-MM-dd HH:mm:ss", culture, DateTimeStyles.None, out startDate) &&
                DateTime.TryParseExact(input.Substring(19), "yyyy-MM-dd HH:mm:ss", culture, DateTimeStyles.None, out endDate))
            {
                return (startDate, endDate);
            }
 
            // 尝试解析第二种格式 "2023-01-01T08:02:032023-01-31T08:02:03"
            if (DateTime.TryParseExact(input.Substring(0, 19), "yyyy-MM-ddTHH:mm:ss", culture, DateTimeStyles.None, out startDate) &&
                DateTime.TryParseExact(input.Substring(19), "yyyy-MM-ddTHH:mm:ss", culture, DateTimeStyles.None, out endDate))
            {
                return (startDate, endDate);
            }
            // 如果都不是,返回null
            return (null, null);
        }
 
        /// <summary>
        /// 移除最后一个字符的?号
        /// </summary>
        /// <param name="input"> </param>
        /// /// <param name="inChar">字符</param>
        /// <returns></returns>
        public static string RemoveCharIfPresent(this string input, char inChar)
        {
            if (string.IsNullOrEmpty(input)) return input;
 
            // 检查字符串的最后一个字符是否为问号
            if (input[^1] == '?')
            {
                // 使用切片操作符创建不包含问号的新字符串
                return input[..^1];
            }
            else
            {
                // 字符串不以问号结尾,返回原字符串
                return input;
            }
        }
 
        /// <summary>
        /// 移除最后的字符串
        /// </summary>
        /// <param name="input"></param>
        /// <param name="endString"></param>
        /// <returns></returns>
        public static string RemoveEndStringIfExists(this string input, string endString)
        {
            if (string.IsNullOrEmpty(input) || string.IsNullOrEmpty(endString))
            {
                return input; // 如果输入字符串或结束字符串为空或null,直接返回原字符串
            }
 
            if (input.EndsWith(endString))
            {
                return input.Substring(0, input.Length - endString.Length); // 移除结束字符串
            }
 
            return input; // 如果不以指定字符串结束,返回原字符串
        }
 
 
        /// <summary>
        /// 首字母转小写
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string FirstCharToLower(this string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return input;
            }
 
            // 使用正则表达式
            return Regex.Replace(input, @"^\p{Lu}", match => match.Value.ToLower());
        }
        #endregion
 
        /// <summary>
        /// 首字母转大写
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string FirstToCapitalize(this  string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return input;
            }
 
            TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;
            return textInfo.ToTitleCase(input.ToLower()).Substring(0, 1) + input.Substring(1);
        }
    }
}