What is the principle of string comparisons?
Principle:
Start by comparing the first character of the two strings (size comparison by the ASCII value of the characters) until a different character occurs or a '/s ' is encountered.
If all characters are the same, the two strings are considered equal, returning 0;
If there is a different character, the first character compares the result, if the former word sizes to the latter, then returns 1, otherwise returns-1.
Attention:
The order is the first object compared to the second object!
Before > after return 1;
Front = after return 0;
Front < rear return-1
The CompareTo method is the same as the function implemented by the Compare method, except that the position of the parameter is different.
eg. s1.compareto (S2):
Represents the size comparison of the string S1 with S2,
S1<s2 S1.compareto (S2) Results: 1
S1=s2 S1.compareto (s2) Results: 0
S1>s2 S1.compareto (S2) Results: 1
eg. String.Compare (S1,S2) This method shows the same function as above, and even the return value results.
Represents the size comparison of the string S1 with S2,
S1<s2 String.Compare (S1,S2) Results: 1
S1=s2 String.Compare (S1,S2) Results: 0
S1>s2 String.Compare (S1,S2) Results: 1
The code is compared as follows:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceconsoleapplication4{classProgram {Static voidMain (string[] args) { stringS1 ="a"; stringS2 ="b"; Console.WriteLine ("S1.compareto (S2):"+S1.compareto (S2)); stringS3 ="Foobar"; stringS4 ="Foofoo";//behind the BigConsole.WriteLine ("S3.compareto (S4):"+S3.compareto (S4)); stringS5 ="Foobar"; stringS6 ="Foofoo"; intVal =String.Compare (S5, S6); Console.WriteLine ("String.Compare (S5,S6):"+Val); S6="Fooaar"; Val=String.Compare (S5, S6); Console.WriteLine ("String.Compare (S5,S6):"+Val); S6="Foobar"; Val=String.Compare (S5, S6); Console.WriteLine ("String.Compare (S5,S6):"+Val); Console.readkey (); } }}
String size comparison function--compareto and compare methods in C # (needs to be complemented)