String size comparison function--compareto and compare methods in C # (needs to be complemented)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.