1Staticvoid Main (string[] args)2 {3 strings ="";4 //(1) Character access (subscript access s[i])5s ="ABCD";6Console.WriteLine (s[0]);//output "A";7Console.WriteLine (s.length);//Output 48 Console.WriteLine ();9 //(2) break into character array (ToCharArray)Tens ="ABCD"; One Char[] arr = S.tochararray ();//break the string into character array {' A ', ' B ', ' C ', ' D '} AConsole.WriteLine (arr[0]);//The first element of the output array, output "A" - Console.WriteLine (); - //(3) Intercept sub-string (Substring) thes ="ABCD"; -Console.WriteLine (S.substring (1));//start at 2nd bit (index starting from 0) intercept until end of string, output "BCD" -Console.WriteLine (S.substring (1,2));//2 bits starting from 2nd bit, 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" AConsole.WriteLine (S.indexof ("BCD"));//Search the position of the first matching string BCD from the beginning of the string, output "4" atConsole.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"));//Search from the end of the string for the position of the first matching string bcd, Output "3" -Console.WriteLine (S.indexof ('E'));//The position of the first matched string e is searched from the beginning of the string, with no matching output "1"; -Console.WriteLine (S.contains ("ABCD"));//determines if another string "ABCD" is present in the string, outputting true - Console.WriteLine (); - //(5) Case conversion (ToUpper and ToLower) ins ="ABcD"; -Console.WriteLine (S.tolower ());//convert to lowercase, output "ABCD" toConsole.WriteLine (S.toupper ());//convert to uppercase, output "ABCD" + Console.WriteLine (); - //(6) Fill alignment (PadLeft and PadRight) thes ="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__"Panax Notoginseng Console.WriteLine (); - //(7) Cutting head to Tail (Trim) thes ="__ab__cd__"; +Console.WriteLine (S.trim ('_'));//Remove the ' _ ' character from the string in the header and tail, output "AB__CD" AConsole.WriteLine (S.trimstart ('_'));//Remove the ' _ ' character of the header in the string and output "ab__cd__" theConsole.WriteLine (S.trimend ('_'));//Remove the trailing ' _ ' character from the string and output "__AB__CD" + Console.WriteLine (); - //(8) inserting and removing (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" the Console.WriteLine (s); -Console.WriteLine (S.remove (0,2));//Remove 2 characters from the 1th bit of the string, output "EF"Wuyi Console.WriteLine (); the //(9) Replacement character (string) (replace) -s ="a_b_c_d"; WuConsole.WriteLine (S.replace ('_','-'));//Replace the ' _ ' character in the string with '-' and output ' a-b-c-d ' -Console.WriteLine (S.replace ("_",""));//Replace "_" in the string with an empty string, output "A B C D" About 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 a string with ', ' characters, returns an array of strings -Console.WriteLine (arr1[0]);//output "AA" AConsole.WriteLine (arr1[1]);//output "BB" +Console.WriteLine (arr1[2]);//output "CC" theConsole.WriteLine (arr1[3]);//output "DD" - Console.WriteLine (); $s ="AA--BB--CC--DD"; the string[] arr2 = S.replace ("--","-"). Split ('-');//tips for splitting with strings: first replace the string "--" with a single character "-" and then split the string with the '-' character to return an array of strings theConsole.WriteLine (arr2[0]);//output "AA" theConsole.WriteLine (arr2[1]);//output "BB" theConsole.WriteLine (arr2[2]);//output "CC" -Console.WriteLine (arr2[3]);//output "DD" in Console.WriteLine (); the //(11) Formatting (static method format) theConsole.WriteLine (string. Format ("{0} + {1} = {2}",1,2,1+2)); AboutConsole.WriteLine (string. Format ("{0}/{1} = {2:0.000}",1,3,1.00/3.00)); theConsole.WriteLine (string. Format ("{0:yyyy mm month DD Day}", DateTime.Now)); the //(12) concatenate into a string (static method Concat, static method join, and instance method Stringbuilder.append) thes ="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 that outputs "ABCD" theConsole.WriteLine (string. Join (",", ARR3));//concatenate a string array into a string with "," as a split symbol, output "a,b,c,d"BayiStringBuilder SB =NewStringBuilder ();//declares a string constructor instance theSb. Append ("A");//using string constructors to connect strings for higher performance theSb. Append ('B'); -Console.WriteLine (sb.) ToString ());//output "AB" - Console.readkey (); the}
C # Action String method Summary < Go >