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 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)); 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"
Console.WriteLine (S.indexof ("BCD")); Search the position of the first matching string BCD from the beginning of the string, 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")); 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)
s = "ABcD";
Console.WriteLine (S.tolower ()); Convert 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 tail (Trim)
s = "__ab__cd__";
Console.WriteLine (S.trim ('_')); Remove the ' _ ' character from the string in the header and tail, output "AB__CD"
Console.WriteLine (S.trimstart ('_')); Remove the ' _ ' character of the header in the string and output "ab__cd__"
Console.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"
Console.WriteLine (s);
Console.WriteLine (S.remove (0, 2)); Remove 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 '-' and output ' a-b-c-d '
Console.WriteLine (S.replace ("_", "" ")); Replace "_" in the string with an empty string, 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 a 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 ('-'); 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
Console.WriteLine (Arr2[0]); Output "AA"
Console.WriteLine (Arr2[1]); Output "BB"
Console.WriteLine (Arr2[2]); Output "CC"
Console.WriteLine (Arr2[3]); Output "DD"
Console.WriteLine ();
(11) Formatting (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 that outputs "ABCD"
Console.WriteLine (String. Join (",", ARR3)); Concatenate a string array 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 string constructors to connect strings for higher performance
Sb. Append (' B ');
Console.WriteLine (sb.) ToString ());//Output "AB"
Console.readkey ();
}
C # Action String method Summary < Go >