The following methods generally have many overloaded forms. As a beginner, let me record the ones I have used. . . Others can be added little by little in the future;
Go straight to the example. First define two strings str1, str2 (don't complain about naming ==)
string str1,str2;
Get the length of the string
int string.Length{get;};
1 str1="012345abc"; 2 Console.WriteLine(str1.Length);
//output:9
convert case
1 string string .ToUpper(); // turn to uppercase 2 string string .ToLower(); // turn to lowercase
str1 = " 123abcABC one two three " ; Console.WriteLine(str1.ToUpper()); Console.WriteLine(str1.ToLower()); // output: 123ABCABC one two three 123abcabc one two three
compare strings
bool string.Equals(string); bool Equals(object,object);
1 str1 = " 123ABC " ; 2 ste2 = " 123abc " ; 3 4 Console.WriteLine(str1.Equals(str2)); // return false 5 Console.WriteLine(Equals(str1,str2)); // return false 6 Console.WriteLine(str1.Equals(str2,StringComparison.Ordin alIgnoreCase)); // Return true 7 // Among them, StringComparison.OrdinalIgnoreCase enumeration, ignoring case comparison
split string
1 string[] string.Split(params char[]);
str1 = " 123abc " ; string [] newSte = str1.Split(' a' ); foreach ( var item in newStr ) { Console.WriteLine(item); } /* The output is: 123 bc */
Because the parameter type is modified by params, the Split() method can directly pass in multiple characters instead of just character arrays, such as
str.Split('a','2');
Then he will be divided into three segments based on a and 2.
Check if substring exists
bool string.Contains(string);
not much to say
Determine the beginning and end, obtain the string position and intercept the string
These four 666, combined with outstanding effect
bool string .StartWith( string ); // Judge whether the beginning is the input parameter bool string .EndWith( string ); // Judge whether the end is the input parameter int string .IndexOf( string ); // Get the position where the substring first appears string string .SubString( int length); // Intercept the substring string string .SubString( int start, int lenhth) ;
1 string str = " <sc>ascdsc " ; 2 if (str.StartsWith( " < " )) // if the beginning is < 3 { 4 if (!str.EndsWith( " > " )) // if the end is not > 5 { 6 int n = str.IndexOf( " > " ); 7 string ans = str.Substring(n + 1 ); 8 Console. WriteLine(ans); 9 } 10 } 11 //The output is: ascdsc
Reprinted in: https://www.cnblogs.com/Yukisora/p/7017208.html