C# string 常用功能的方法扩展
程序员文章站
2022-05-16 10:21:58
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 us... ......
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 /// <returns>the input string formatted by using the regex string</returns> 371 public static string regexformat(this string input, string format, string outputformat, regexoptions options = regexoptions.none) 372 { 373 input.throwifnullorempty("input"); 374 return regex.replace(input, format, outputformat, options); 375 } 376 377 #endregion 378 379 #region 转换 380 /// <summary> 381 /// 全角转半角 382 /// </summary> 383 /// <param name="input">要转换的字符串</param> 384 /// <returns>转换完的字符串</returns> 385 public static string narrow(this string input) 386 { 387 return strings.strconv(input, vbstrconv.narrow, 0); 388 } 389 /// <summary> 390 /// 半角转全角 391 /// </summary> 392 /// <param name="input">要转换的字符串</param> 393 /// <returns>转换完的字符串</returns> 394 public static string wide(this string input) 395 { 396 return strings.strconv(input, vbstrconv.wide, 0); 397 } 398 /// <summary> 399 /// 简体转繁体 400 /// </summary> 401 /// <param name="input"></param> 402 /// <returns></returns> 403 public static string traditionalchinese(this string input) 404 { 405 return strings.strconv(input, vbstrconv.traditionalchinese, 0); 406 } 407 /// <summary> 408 /// 繁体转简体 409 /// </summary> 410 /// <param name="input"></param> 411 /// <returns></returns> 412 public static string simplifiedchinese(this string input) 413 { 414 return strings.strconv(input, vbstrconv.simplifiedchinese, 0); 415 } 416 /// <summary> 417 /// 将每个单词首字母大写 418 /// </summary> 419 /// <param name="input"></param> 420 /// <returns></returns> 421 public static string propercase(this string input) 422 { 423 return strings.strconv(input, vbstrconv.propercase, 0); 424 } 425 #endregion 426 427 #endregion 428 } 429 }