This article mainly introduces the C # operation string Method Summary instance code, the need for friends can refer to the following
Needless to say, the specific code is described as follows:
Staticvoid Main (string[] args) {string s = ""; (1) Character access (subscript access s[i]) s = "ABCD"; Console.WriteLine (S[0]); Output "A"; Console.WriteLine (s.length); Output 4 Console.WriteLine (); (2) break into a character array (ToCharArray) s = "ABCD"; char[] arr = S.tochararray (); Break the string into character array {' A ', ' B ', ' C ', ' D '} Console.WriteLine (Arr[0]); The first element of the output array, output "A" Console.WriteLine (); (3) Intercept sub-string (Substring) s = "ABCD"; Console.WriteLine (s.substring (1)); Starts at the 2nd bit (index starting from 0) and intercepts until the end of the string, outputting "BCD" Console.WriteLine (s.substring (1, 2)); Intercept 2 bits starting from the 2nd position, output "BC" Console.WriteLine (); (4) Matching index (INDEXOF ()) s = "ABCABCD"; Console.WriteLine (S.indexof (' A ')); Search for the first matching character A's position index from the beginning of the string, output "0" Console.WriteLine (S.indexof ("BCD")); From the beginning of the string, search for the position of the first matching string bcd, Output "4" Console.WriteLine (S.lastindexof (' C ')); Search for the position of the first match character C from the end of the string, output "5" Console.WriteLine (S.lastindexof ("AB")); Searches for the position of the first matching string BCD from the end of the string, outputting "3" Console.WriteLine (S.indexof (' E ')); Search for the first matching word from the beginning of the stringThe position of the string e, no matching output "-1"; Console.WriteLine (S.contains ("ABCD")); Determine if there is another string "ABCD" in the string, outputting true Console.WriteLine (); (5) Case conversion (ToUpper and tolower) s = "ABcD"; Console.WriteLine (S.tolower ()); Converted to lowercase, output "ABCD" Console.WriteLine (S.toupper ()); Convert to uppercase, output "ABCD" Console.WriteLine (); (6) Fill alignment (PadLeft and padright) s = "ABCD"; Console.WriteLine (S.padleft (6, ' _ ')); Fill the left part of the string with ' _ ' to extend it to 6-bit total length, output "__ABCD" Console.WriteLine (S.padright (6, ' _ ')); Fill the right part of the string with ' _ ' to extend it to 6-bit total length, output "abcd__" Console.WriteLine (); (7) Cutting head to the tail (Trim) s = "__ab__cd__"; Console.WriteLine (S.trim ('_')); Remove the ' _ ' character of the string in the head and tail, output "Ab__cd" Console.WriteLine (S.trimstart ('_')); Remove the ' _ ' character of the header in the string, output "ab__cd__" Console.WriteLine (S.trimend ('_')); Remove the trailing ' _ ' character in the string, output "__ab__cd" Console.WriteLine (); (8) Insert and remove (insert and remove) s = "adef"; Console.WriteLine (S.insert (1, "BC")); Insert the string "BC" in the 2nd place of the string, output "ABCDEF" Console.WriteLine (s); Console.WriteLine (S.remove (1)); From the beginning of the 2nd bit of the string to the last character is deleted, output "A" Console.WriteLine (s); Console.WriteLine (S.remove (0, 2)); Delete 2 characters from the 1th bit of the string, output "EF" Console.WriteLine (); (9) Replacement character (string) (replace) s = "a_b_c_d"; Console.WriteLine (S.replace (' _ ', '-')); Replace the ' _ ' character in the string with '-', Output ' a-b-c-d ' Console.WriteLine (S.replace ("_", "")); Replace "_" in the string with an empty string, and output "A B C D" Console.WriteLine (); (10) split into a string array (split)-reciprocal operation: union A string static method join (seperator,arr[]) s = "AA,BB,CC,DD"; string[] arr1 = S.split (', '); Splits the string with ', ' characters, returns an array of strings Console.WriteLine (Arr1[0]); Output "AA" Console.WriteLine (Arr1[1]); Output "BB" Console.WriteLine (Arr1[2]); Output "CC" Console.WriteLine (Arr1[3]); Output "DD" Console.WriteLine (); s = "AA--BB--CC--DD"; string[] arr2 = S.replace ("--", "-"). Split ('-'); Techniques for splitting with strings: first replace the string "--" with a single character "-" and then split the string with the '-' character, returning the string array Console.WriteLine (Arr2[0]); Output "AA" Console.WriteLine (Arr2[1]); Output "BB" Console.WriteLine (Arr2[2]); Lose"CC" Console.WriteLine (Arr2[3]); Output "DD" Console.WriteLine (); (11) Format (static method Format) Console.WriteLine (String. Format ("{0} + {1} = {2}", 1, 2, 1+2)); Console.WriteLine (String. Format ("{0}/{1} = {2:0.000}", 1, 3, 1.00/3.00)); Console.WriteLine (String. Format ("{0:yyyy mm month DD day}", DateTime.Now)); (12) Concatenate into a string (static method Concat, static method join and instance method Stringbuilder.append) s = "a,b,c,d"; string[] Arr3 = S.split (', '); arr = {"A", "B", "C", "D"} Console.WriteLine (String. Concat (ARR3)); Concatenate a string array into a string, outputting the "ABCD" Console.WriteLine (String. Join (",", ARR3)); A string array is concatenated into a string with "," as a split symbol, output "A,b,c,d" StringBuilder SB =new StringBuilder (); Declares a string constructor instance sb. Append ("A"); Using the string constructor to connect strings can get a higher performance of SB. Append (' B '); Console.WriteLine (sb.) ToString ());//Output "AB" Console.readkey (); }