C # string method,
Author: Chang Hao
5 staticvoid Main (string [] args) 6 {7 string s = ""; 8 // (1) character access (subscript access s [I]) 9 s = "ABCD"; 10 Console. writeLine (s [0]); // output "A"; 11 Console. writeLine (s. length); // output 4 12 Console. writeLine (); 13 14 // (2) split into character arrays (ToCharArray) 15 s = "ABCD"; 16 char [] arr = s. toCharArray (); // splits the string into character arrays {'A', 'B', 'C', 'D'} 17 Console. writeLine (arr [0]); // outputs the first element of the array and outputs "A" 18 Console. writeLine (); 19 20 // (3) Capture Substring (Substring) 21 s = "ABCD"; 22 Console. writeLine (s. substring (1); // starts from 2nd bits (index starts from 0) until the string ends, and outputs "BCD" 23 Console. writeLine (s. substring (1, 2); // truncates two digits from the first digit and outputs "BC" 24 Console. writeLine (); 25 26 // (4) matching index (IndexOf () 27 s = "ABCABCD"; 28 Console. writeLine (s. indexOf ('A'); // search for the index of the First Matching character A from the string header, and output "0" 29 Console. writeLine (s. indexOf ("BCD"); // search for the position of the first matching string BCD starting from the string header, and output "4" 30 Console. writeLine (s. lastIndexOf ('C'); // search for the position of the First Matching character C from the end of the string, and output "5" 31 Console. writeLine (s. lastIndexOf ("AB"); // search for the position of the first BCD matching string starting from the end of the string, and output "3" 32 Console. writeLine (s. indexOf ('E'); // search for the first matching string E position from the string header without matching the output "-1"; 33 Console. writeLine (s. contains ("ABCD"); // checks whether another string "ABCD" exists in the string and outputs the true 34 Console. writeLine (); 35 36 // (5) case-sensitive conversion (ToUpper and ToLower) 37 s = "aBcD"; 38 Console. writeLine (s. toLower (); // converts it to lowercase and outputs "abcd" 39 Console. writeLine (s. toUpper (); // converts it to uppercase and outputs "ABCD" 40 Console. writeLine (); 41 42 // (6) padding alignment (PadLeft and PadRight) 43 s = "ABCD"; 44 Console. writeLine (s. padLeft (6, '_'); // use '_' to fill the left of the string and extend it to the total length of 6 characters. The output is "_ ABCD" 45 Console. writeLine (s. padRight (6, '_'); // use '_' to fill the right of the string and extend it to the total length of 6 bits. The output is "ABCD _" 46 Console. writeLine (); 47 48 // (7) truncation (Trim) 49 s = "_ abcd cd _"; 50 Console. writeLine (s. trim ('_'); // remove the '_' character from the header and tail of the string, and output "ABCD cd" 51 Console. writeLine (s. trimStart ('_'); // removes the '_' character from the string header and outputs "abcd_52 Console. writeLine (s. trimEnd ('_'); // remove the '_' character at the end of the string and output "_ ABCD cd" 53 Console. writeLine (); 54 55 // (8) Insert and delete (Insert and Remove) 56 s = "ADEF"; 57 Console. writeLine (s. insert (1, "BC"); // Insert the string "BC" at 2nd bits, and output "ABCDEF" 58 Console. writeLine (s); 59 Console. writeLine (s. remove (1); // Delete All characters starting from the first character of the string and output "A" 60 Console. writeLine (s); 61 Console. writeLine (s. remove (0, 2); // Delete 2 characters from the string's 1st bits and output "EF" 62 Console. writeLine (); 63 64 // (9) Replace character (string) (Replace) 65 s = "A_ B _C_D"; 66 Console. writeLine (s. replace ('_', '-'); // Replace the '_' character in the string with '-', and output "A-B-C-D" 67 Console. writeLine (s. replace ("_", ""); // Replace "_" in the string with an empty string and output "a B C D" 68 Console. writeLine (); 69 70 // (10) Split into a String Array (Split) -- reciprocal operation: Join a string static method (seperator, arr []) 71 s = "AA, BB, CC, DD"; 72 string [] arr1 = s. split (','); // use the ',' character to separate the string. Return the string array 73 Console. writeLine (arr1 [0]); // output "AA" 74 Console. writeLine (arr1 [1]); // output "BB" 75 Console. writeLine (arr1 [2]); // output "CC" 76 Console. writeLine (arr1 [3]); // output "DD" 77 Console. writeLine (); 78 79 s = "AA--BB--CC--DD"; 80 string [] arr2 = s. replace ("--","-"). split ('-'); // string-based splitting technique: First Replace the string "--" with a single character "-", split the string with the '-' character and return the string array 81 Console. writeLine (arr2 [0]); // output "AA" 82 Console. writeLine (arr2 [1]); // output "BB" 83 Console. writeLine (arr2 [2]); // output "CC" 84 Console. writeLine (arr2 [3]); // output "DD" 85 Console. writeLine (); 86 87 // (11) Format (static method Format) 88 Console. writeLine (string. format ("{0} + {1} = {2}", 1, 2, 1 + 2); 89 Console. writeLine (string. format ("{0}/{1} = {2: 0. 000} ", 1, 3, 1.00/3.00); 90 Console. writeLine (string. format ("{0: MM dd, yyyy}", DateTime. (Now); 91 92 93 // (12) concatenate a string (static Concat, static Join, and StringBuilder. append) 94 s = "A, B, C, D"; 95 string [] arr3 = s. split (','); // arr = {"A", "B", "C", "D"} 96 97 Console. writeLine (string. concat (arr3); // concatenates a string array into a string and outputs "ABCD" 98 99 Console. writeLine (string. join (",", arr3); // use "," as the separator to concatenate A string array into A string and output "A, B, C, D "100 101 StringBuilder sb = new StringBuilder (); // declare a string constrtor instance 102 sb. append ("A"); // use the string constructor to connect to the string for higher performance of 103 sb. append ('B'); 104 Console. writeLine (sb. toString (); // output "AB" 105 106 Console. readKey (); 107}