XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension

【DLL】XFE各类拓展是一个C#的DLL库,旨在优化C#代码中常用语句的使用,并提供更简洁的访问方式,同时提供Xunit测试框架,快速搭建服务器/客户端,免费ChatGPTAPI接口,免费通讯服务器,XFE下载器,新增格式等

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/XFEExtension

Delete XFE各类拓展/StringExtention.cs

9cb5cc2
Tom Smith <132526994+XFEstudio@users.noreply.github.com>
提交于

代码差异

1 个文件 +0 -205
Deleted XFE各类拓展/StringExtention.cs +0 -205
@@ -1,205 +0,0 @@
1 using System;
2 using System.Text;
3 using System.Text.RegularExpressions;
4
5 namespace XFE各类拓展.StringExtension
6 {
7 namespace Json
8 {
9 /// <summary>
10 /// 对string类进行Json操作的扩展
11 /// </summary>
12 public static class JsonStringExtension
13 {
14 /// <summary>
15 /// 根据给定的开头和末尾返回查找到的第一个匹配的字符串(全匹配)
16 /// </summary>
17 /// <param name="str">被匹配的字符串</param>
18 /// <param name="beginstr">匹配开头字符串</param>
19 /// <param name="endstr">匹配结尾字符串</param>
20 /// <returns>返回夹在开头和末尾中间的字符串</returns>
21 public static string GetStringBetweenTwoString(this string str, string beginstr, string endstr)
22 {
23 if (str != string.Empty && str != null)
24 {
25 int beginindex = str.IndexOf(beginstr, StringComparison.Ordinal);
26 if (beginindex == -1 || beginindex == 0)
27 {
28 return string.Empty;
29 }
30 int endindex = str.IndexOf(endstr, beginindex, StringComparison.Ordinal);
31 if (endindex == -1 || endindex == 0)
32 {
33 return string.Empty;
34 }
35 return str.Substring(beginindex + beginstr.Length, endindex - beginindex - beginstr.Length);
36 }
37 else
38 {
39 return string.Empty;
40 }
41 }
42 /// <summary>
43 /// 通过给定的文本格式查找对应的字段
44 /// </summary>
45 /// <param name="str">待匹配文本</param>
46 /// <param name="form">查找的文本格式</param>
47 /// <returns></returns>
48 public static string GetTextByForm(this string str, string form)
49 {
50 return GetStringBetweenTwoString(str, form + ":", ",");
51 }
52 }
53 }
54 /// <summary>
55 /// 字符串拓展类
56 /// </summary>
57 public static class StringExtension
58 {
59 /// <summary>
60 /// 替换字符串中的多个字符串为指定字符串
61 /// </summary>
62 /// <param name="str"></param>
63 /// <param name="replaceStringArray">替换的字符串组</param>
64 /// <param name="tarString">目的字符串</param>
65 /// <returns></returns>
66 public static string ReplaceStrings(this string str, string[] replaceStringArray, string tarString)
67 {
68 foreach (string replaceString in replaceStringArray)
69 {
70 str = str.Replace(replaceString, tarString);
71 }
72 return str;
73 }
74 /// <summary>
75 /// 匹配两个字符串之间的字符串
76 /// </summary>
77 /// <param name="input"></param>
78 /// <param name="startString">起始字符串</param>
79 /// <param name="endString">结束字符串</param>
80 /// <returns></returns>
81 public static string[] MatchStringsBetween(this string input, string startString, string endString)
82 {
83 string pattern = $"{Regex.Escape(startString)}(.*?){Regex.Escape(endString)}";
84 MatchCollection matches = Regex.Matches(input, pattern);
85
86 string[] result = new string[matches.Count];
87 for (int i = 0; i < matches.Count; i++)
88 {
89 result[i] = matches[i].Groups[1].Value;
90 }
91
92 return result;
93 }
94 /// <summary>
95 /// 文本自动换行
96 /// </summary>
97 /// <param name="text"></param>
98 /// <param name="width">一行文本的最大长度</param>
99 /// <returns></returns>
100 public static string WrapText(this string text, int width)
101 {
102 StringBuilder sb = new StringBuilder();
103 int startIndex = 0;
104 while (startIndex < text.Length)
105 {
106 // 寻找下一个换行位置
107 int endIndex = startIndex + width;
108 if (endIndex >= text.Length)
109 {
110 // 如果已经到达文本末尾,则直接添加剩余部分并结束循环
111 sb.Append(text.Substring(startIndex));
112 break;
113 }
114 else
115 {
116 // 在指定宽度范围内寻找最后一个空格字符
117 int lastSpaceIndex = text.LastIndexOf(' ', endIndex, width);
118 if (lastSpaceIndex > startIndex)
119 {
120 // 如果找到了空格字符,则将文本从起始位置到空格字符位置添加到结果中,并进行换行
121 sb.Append(text.Substring(startIndex, lastSpaceIndex - startIndex));
122 sb.AppendLine();
123 startIndex = lastSpaceIndex + 1;
124 }
125 else
126 {
127 // 如果没有找到空格字符,则直接添加指定宽度的文本到结果中,并进行换行
128 sb.Append(text.Substring(startIndex, width));
129 sb.AppendLine();
130 startIndex += width;
131 }
132 }
133 }
134
135 return sb.ToString();
136 }
137 /// <summary>
138 /// 输出到控制台并返回
139 /// </summary>
140 /// <param name="obj"></param>
141 /// <returns>ToString后的内容</returns>
142 public static string ConsoleWriteLine(this object obj)
143 {
144 Console.WriteLine(obj);
145 return obj.ToString();
146 }
147 #region 返回包含分割器的字符串
148 /// <summary>
149 /// 返回包含分割器的字符串
150 /// </summary>
151 /// <param name="str"></param>
152 /// <param name="delimiter">分割器组</param>
153 /// <param name="removeEmptyString">是否移除空字符串</param>
154 /// <returns>分割后的带分割器字符串数组</returns>
155 public static string[] SplitAndKeepDelimiter(this string str, string[] delimiter, bool removeEmptyString)
156 {
157 string[] result;
158 if (removeEmptyString)
159 {
160 result = str.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
161 }
162 else
163 {
164 result = str.Split(delimiter, StringSplitOptions.None);
165 }
166 for (int i = 0; i < result.Length - 1; i++)
167 {
168 result[i] += delimiter;
169 }
170 return result;
171 }
172 /// <summary>
173 /// 返回包含分割器的字符串
174 /// </summary>
175 /// <param name="str"></param>
176 /// <param name="delimiter">分割器</param>
177 /// <param name="removeEmptyString">是否移除空字符串</param>
178 /// <returns>分割后的带分割器字符串数组</returns>
179 public static string[] SplitAndKeepDelimiter(this string str, string delimiter, bool removeEmptyString)
180 {
181 return SplitAndKeepDelimiter(str, new string[] { delimiter }, removeEmptyString);
182 }
183 /// <summary>
184 /// 返回包含分割器的字符串
185 /// </summary>
186 /// <param name="str"></param>
187 /// <param name="delimiter">分割器</param>
188 /// <returns>分割后的带分割器字符串数组</returns>
189 public static string[] SplitAndKeepDelimiter(this string str, string delimiter)
190 {
191 return SplitAndKeepDelimiter(str, new string[] { delimiter }, true);
192 }
193 /// <summary>
194 /// 返回包含分割器的字符串
195 /// </summary>
196 /// <param name="str"></param>
197 /// <param name="delimiter">分割器组</param>
198 /// <returns>分割后的带分割器字符串数组</returns>
199 public static string[] SplitAndKeepDelimiter(this string str, string[] delimiter)
200 {
201 return SplitAndKeepDelimiter(str, delimiter, true);
202 }
203 #endregion
204 }
205 }