C # common usage of string,
1 --- "string declaration:
1. string s = new string (char [] arr) // declare a string based on a character array, which converts a character group to a string.
2. string s = new string (char r, int I) // generate an I character r string.
2 --- common static methods of character strings:
1. Comparison of Compare strings (in alphabetical order)
Int result = string. Compare (string str1, string str2 );
When str1> str2, 1 is returned.
When str1 = str2, 0 is returned.
-1 is returned when str1 <str2
String. Compare (string str1, string str2, bool ignoreCase) // case-insensitive comparison
2. There are many Concat connection method parameters. Commonly Used Concat (string str1, string str2 );
String str = string. Concat ("w", "e"); // str = "we ";
3. Format parameter processing, equivalent to Console. WriteLine ();
String str = String. Format ("today {0} Very Hot", "Weather"); // str = "today's weather is very hot ";
4. IsNullOrEmpty checks whether the character is null or null, and returns bool;
String str1 = "hahha ";
Bool b1 = string. IsNullOrEmpty (str); // b1 = false;
String str2 = "";
Bool b2 = string. IsNullOrEmpty (str2); // b2 = true;
String str3 = null;
Bool b3 = string. IsNullOrEmpty (str3); // b3 = true;
5. Merge Join strings
String. Join (string str, string [] strArr); // Concatenates the content of the strArr array into a new string, and adds the separator str between each two items of the corresponding array.
String strs = string. join (",", string [] {"w", "e", "r", "t"}); // strs = "w, e, r, t ";
3 --- "string common instance methods:
1. Contains checks whether a string Contains a certain character and returns the bool value.
String str = "tired ";
Bool B = str. Contains ("tired"); // B = true;
2. EndsWith and StartsWith determine whether a certain string has started or ended.
String str = "great rain ";
Bool b1 = str. StartsWith ("big"); // b1 = false;
Bool b2-str.EndsWith ("ah"); // b2 = true;
3. Equals compares the two strings to determine if they are equal.
String str1 = "asd ";
String str2 = "ert ";
Bool B = str1.Equals (str2); // B = false;
Bool <strName>. Equals (string str, StringComparison. OrdinalIgnoreCase) // indicates case insensitive
4. IndexOf and LastIndexOf determine the position of the first occurrence (IndexOf) and last occurrence (LastIndexOf) of a string. If not, the return value is-1.
String str = "Today's rain is heavy, it's cold ";
Int I = str. IndexOf ("very"); // I = 4;
Int I = str. LastIndexOf ("very"); // j = 8;
Int m = str. IndexOf ("small"); // m =-1;
5. Replace the string (the character can also be replaced) and return a new string.
String str = "sleepy ";
String s = str. Replace ("sleepy", "Spirit"); // s = "good spirit ";
6. Insert
Insert characters at the index position of the string. The original characters are removed and changed to a new string.
String str = "Late Night ";
String s = str. Insert (1, "already"); // s = "The night is already deep"
7. Remove a string (character)
Remove the string with length starting from startIndex and remaining characters into a new string (<strName >=< strName>. Remove (startIndex, length );)
String str = "The night is already deep ";
String s = str. Remove (1, 2); // s = "Late Night ";
8. Split splits the string <strName> into characters in the sep array and stores the obtained content in an array (string [] <strName>. split (params char [] sep );)
String str = "I, really, sleepy; Ah ";
String [] s = str. split (new char () {','; '}); // s = string [] {"I", "true ", "sleepy", "Ah "};
9. The Substring truncation character <str> starts with index and intercepts lenth characters (string <str>. Substring (index, lenth ))
String str = "It's still raining ";
String s = str. Substring (2, 2); // s = "Rain ";
10. ToCharArray converts a string to a character array (<string>. ToCharArray ())
String str = "Rain is low ";
Char [] s = str. ToCharArray (); // s = char [] {'rain', "already", "jing", "Xiao", ""};
11. Trim () goes out with spaces on both sides
String str = "dd ";
String s = str. Trim (); // s = "dd ";
12. ToUpper (converted to uppercase) and ToLower (converted to lowercase)
String s = "RaSer ";
String s1 = s. ToUpper (); // s1 = "RASER ";
String s2 = s. ToLower (); // s2 = "raser ";