当前位置:
首页 > Python基础教程 >
-
C#教程之C# string 常用功能的方法扩展
1 #region Usings 2 using System; 3 using System.Text; 4 using System.Data; 5 using System.Data.SqlClient; 6 using System.Text.RegularExpressions; 7 using System.Linq; 8 using System.Collections.Generic; 9 using System.ComponentModel; 10 using DragonUtility.DataTypes.Formatters; 11 using Microsoft.VisualBasic; 12 #endregion 13 14 namespace DragonUtility.DataTypes.ExtensionMethods 15 { 16 /// <summary> 17 /// String extensions 18 /// </summary> 19 public static class StringExtensions 20 { 21 #region Functions 22 23 #region Encode 24 25 /// <summary> 26 /// 编码转换,把字符串从一种编码转换到另一种编码 27 /// </summary> 28 /// <param name="Input">input string</param> 29 /// <param name="OriginalEncodingUsing">The type of encoding the string is currently using (defaults to ASCII)</param> 30 /// <param name="EncodingUsing">The type of encoding the string is converted into (defaults to UTF8)</param> 31 /// <returns>string of the byte array</returns> 32 public static string Encode(this string Input, Encoding OriginalEncodingUsing = null, Encoding EncodingUsing = null) 33 { 34 if (string.IsNullOrEmpty(Input)) 35 return ""; 36 OriginalEncodingUsing = OriginalEncodingUsing.NullCheck(new ASCIIEncoding()); 37 EncodingUsing = EncodingUsing.NullCheck(new UTF8Encoding()); 38 return Encoding.Convert(OriginalEncodingUsing, EncodingUsing, Input.ToByteArray(OriginalEncodingUsing)) 39 .ToEncodedString(EncodingUsing); 40 41 } 42 #endregion 43 44 #region FromBase64 45 46 /// <summary> 47 /// Converts base 64 string based on the encoding passed in 48 /// </summary> 49 /// <param name="Input">Input string</param> 50 /// <param name="EncodingUsing">The type of encoding the string is using (defaults to UTF8)</param> 51 /// <returns>string in the encoding format</returns> 52 public static string FromBase64(this string Input, Encoding EncodingUsing) 53 { 54 if (string.IsNullOrEmpty(Input)) 55 return ""; 56 byte[] TempArray = Convert.FromBase64String(Input); 57 return EncodingUsing.NullCheck(new UTF8Encoding()).GetString(TempArray); 58 } 59 60 /// <summary> 61 /// Converts base 64 string to a byte array 62 /// </summary> 63 /// <param name="Input">Input string</param> 64 /// <returns>A byte array equivalent of the base 64 string</returns> 65 public static byte[] FromBase64(this string Input) 66 { 67 return string.IsNullOrEmpty(Input) ? new byte[0] : Convert.FromBase64String(Input); 68 } 69 70 #endregion 71 72 #region Left 73 74 /// <summary> 75 /// Gets the first x number of characters from the left hand side 76 /// </summary> 77 /// <param name="Input">Input string</param> 78 /// <param name="Length">x number of characters to return</param> 79 /// <returns>The resulting string</returns> 80 public static string Left(this string Input, int Length) 81 { 82 return string.IsNullOrEmpty(Input) ? "" : Input.Substring(0, Input.Length > Length ? Length : Input.Length); 83 } 84 85 #endregion 86 87 #region Right 88 89 /// <summary> 90 /// Gets the last x number of characters from the right hand side 91 /// </summary> 92 /// <param name="Input">Input string</param> 93 /// <param name="Length">x number of characters to return</param> 94 /// <returns>The resulting string</returns> 95 public static string Right(this string Input, int Length) 96 { 97 if (string.IsNullOrEmpty(Input)) 98 return ""; 99 Length = Input.Length > Length ? Length : Input.Length; 100 return Input.Substring(Input.Length - Length, Length); 101 } 102 103 #endregion 104 105 #region ToBase64 106 107 /// <summary> 108 /// Converts from the specified encoding to a base 64 string 109 /// </summary> 110 /// <param name="Input">Input string</param> 111 /// <param name="OriginalEncodingUsing">The type of encoding the string is using (defaults to UTF8)</param> 112 /// <returns>Bas64 string</returns> 113 public static string ToBase64(this string Input, Encoding OriginalEncodingUsing = null) 114 { 115 if (string.IsNullOrEmpty(Input)) 116 return ""; 117 byte[] TempArray = OriginalEncodingUsing.NullCheck(new UTF8Encoding()).GetBytes(Input); 118 return Convert.ToBase64String(TempArray); 119 } 120 121 #endregion 122 123 #region ToByteArray 124 125 /// <summary> 126 /// Converts a string to a byte array 127 /// </summary> 128 /// <param name="Input">input string</param> 129 /// <param name="EncodingUsing">The type of encoding the string is using (defaults to UTF8)</param> 130 /// <returns>the byte array representing the string</returns> 131 public static byte[] ToByteArray(this string Input, Encoding EncodingUsing = null) 132 { 133 return string.IsNullOrEmpty(Input) ? null : EncodingUsing.NullCheck(new UTF8Encoding()).GetBytes(Input); 134 } 135 136 #endregion 137 138 #region ToFirstCharacterUpperCase 139 140 /// <summary> 141 /// Takes the first character of an input string and makes it uppercase 142 /// </summary> 143 /// <param name="Input">Input string</param> 144 /// <returns>String with the first character capitalized</returns> 145 public static string ToFirstCharacterUpperCase(this string Input) 146 { 147 if (string.IsNullOrEmpty(Input)) 148 return ""; 149 char[] InputChars = Input.ToCharArray(); 150 for (int x = 0; x < InputChars.Length; ++x) 151 { 152 if (InputChars[x] != ' ' && InputChars[x] != '\t') 153 { 154 InputChars[x] = char.ToUpper(InputChars[x]); 155 break; 156 } 157 } 158 return new string(InputChars); 159 } 160 161 #endregion 162 163 #region ToSentenceCapitalize 164 165 /// <summary> 166 /// Capitalizes each sentence within the string 167 /// </summary> 168 /// <param name="Input">Input string</param> 169 /// <returns>String with each sentence capitalized</returns> 170 public static string ToSentenceCapitalize(this string Input) 171 { 172 if (string.IsNullOrEmpty(Input)) 173 return ""; 174 string[] Seperator = { ".", "?", "!" }; 175 string[] InputStrings = Input.Split(Seperator, StringSplitOptions.None); 176 for (int x = 0; x < InputStrings.Length; ++x) 177 { 178 if (!string.IsNullOrEmpty(InputStrings[x])) 179 { 180 Regex TempRegex = new Regex(InputStrings[x]); 181 InputStrings[x] = InputStrings[x].ToFirstCharacterUpperCase(); 182 Input = TempRegex.Replace(Input, InputStrings[x]); 183 } 184 } 185 return Input; 186 } 187 188 #endregion 189 190 #region ToTitleCase 191 192 /// <summary> 193 /// Capitalizes the first character of each word 194 /// </summary> 195 /// <param name="Input">Input string</param> 196 /// <returns>String with each word capitalized</returns> 197 public static string ToTitleCase(this string Input) 198 { 199 if (string.IsNullOrEmpty(Input)) 200 return ""; 201 string[] Seperator = { " ", ".", "\t", System.Environment.NewLine, "!", "?" }; 202 string[] InputStrings = Input.Split(Seperator, StringSplitOptions.None); 203 for (int x = 0; x < InputStrings.Length; ++x) 204 { 205 if (!string.IsNullOrEmpty(InputStrings[x]) 206 && InputStrings[x].Length > 3) 207 { 208 Regex TempRegex = new Regex(InputStrings[x]); 209 InputStrings[x] = InputStrings[x].ToFirstCharacterUpperCase(); 210 Input = TempRegex.Replace(Input, InputStrings[x]); 211 } 212 } 213 return Input; 214 } 215 216 #endregion 217 218 #region NumberTimesOccurs 219 220 /// <summary> 221 /// returns the number of times a string occurs within the text 222 /// </summary> 223 /// <param name="Input">input text</param> 224 /// <param name="Match">The string to match (can be regex)</param> 225 /// <returns>The number of times the string occurs</returns> 226 public static int NumberTimesOccurs(this string Input, string Match) 227 { 228 return string.IsNullOrEmpty(Input) ? 0 : new Regex(Match).Matches(Input).Count; 229 } 230 231 #endregion 232 233 #region Reverse 234 235 /// <summary> 236 /// Reverses a string 237 /// </summary> 238 /// <param name="Input">Input string</param> 239 /// <returns>The reverse of the input string</returns> 240 public static string Reverse(this string Input) 241 { 242 return new string(Input.Reverse<char>().ToArray()); 243 } 244 245 #endregion 246 247 #region FilterOutText 248 249 /// <summary> 250 /// Removes the filter text from the input. 251 /// </summary> 252 /// <param name="Input">Input text</param> 253 /// <param name="Filter">Regex expression of text to filter out</param> 254 /// <returns>The input text minus the filter text.</returns> 255 public static string FilterOutText(this string Input, string Filter) 256 { 257 if (string.IsNullOrEmpty(Input)) 258 return ""; 259 return string.IsNullOrEmpty(Filter) ? Input : new Regex(Filter).Replace(Input, ""); 260 } 261 262 #endregion 263 264 #region KeepFilterText 265 266 /// <summary> 267 /// Removes everything that is not in the filter text from the input. 268 /// </summary> 269 /// <param name="Input">Input text</param> 270 /// <param name="Filter">Regex expression of text to keep</param> 271 /// <returns>The input text minus everything not in the filter text.</returns> 272 public static string KeepFilterText(this string Input, string Filter) 273 { 274 if (string.IsNullOrEmpty(Input) || string.IsNullOrEmpty(Filter)) 275 return ""; 276 Regex TempRegex = new Regex(Filter); 277 MatchCollection Collection = TempRegex.Matches(Input); 278 StringBuilder Builder = new StringBuilder(); 279 foreach (Match Match in Collection) 280 Builder.Append(Match.Value); 281 return Builder.ToString(); 282 } 283 284 #endregion 285 286 #region AlphaNumericOnly 287 288 /// <summary> 289 /// Keeps only alphanumeric characters 290 /// </summary> 291 /// <param name="Input">Input string</param> 292 /// <returns>the string only containing alphanumeric characters</returns> 293 public static string AlphaNumericOnly(this string Input) 294 { 295 return Input.KeepFilterText("[a-zA-Z0-9]"); 296 } 297 298 #endregion 299 300 #region AlphaCharactersOnly 301 302 /// <summary> 303 /// Keeps only alpha characters 304 /// </summary> 305 /// <param name="Input">Input string</param> 306 /// <returns>the string only containing alpha characters</returns> 307 public static string AlphaCharactersOnly(this string Input) 308 { 309 return Input.KeepFilterText("[a-zA-Z]"); 310 } 311 312 #endregion 313 314 #region NumericOnly 315 316 /// <summary> 317 /// Keeps only numeric characters 318 /// </summary> 319 /// <param name="Input">Input string</param> 320 /// <param name="KeepNumericPunctuation">Determines if decimal places should be kept</param> 321 /// <returns>the string only containing numeric characters</returns> 322 public static string NumericOnly(this string Input, bool KeepNumericPunctuation = true) 323 { 324 return KeepNumericPunctuation ? Input.KeepFilterText(@"[0-9\.]") : Input.KeepFilterText("[0-9]"); 325 } 326 327 #endregion 328 329 #region IsUnicode 330 331 /// <summary> 332 /// Determines if a string is unicode 333 /// </summary> 334 /// <param name="Input">Input string</param> 335 /// <returns>True if it's unicode, false otherwise</returns> 336 public static bool IsUnicode(this string Input) 337 { 338 return string.IsNullOrEmpty(Input) ? true : Regex.Replace(Input, @"[^\u0000-\u007F]", "") != Input; 339 } 340 341 #endregion 342 343 #region FormatString 344 345 /// <summary> 346 /// Formats a string based on a format string passed in: 347 /// # = digits 348 /// @ = alpha characters 349 /// \ = escape char 350 /// </summary> 351 /// <param name="Input">Input string</param> 352 /// <param name="Format">Format of the output string</param> 353 /// <returns>The formatted string</returns> 354 public static string FormatString(this string Input, string Format) 355 { 356 return new GenericStringFormatter().Format(Input, Format); 357 } 358 359 #endregion 360 361 #region RegexFormat 362 363 /// <summary> 364 /// Uses a regex to format the input string 365 /// </summary> 366 /// <param name="Input">Input string</param> 367 /// <param name="Format">Regex string used to</param> 368 /// <param name="OutputFormat">Output format</param> 369 /// <param name="Options">Regex options</param> 370
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式