String comparison in. NET 2.0

Source: Internet
Author: User
During code writing, strings may be widely used and many string operations should be used. For string comparison, dotnet2.0 adds an enumerative stringcomparison:


Namespace System
{
Public   Enum Stringcomparison {
Currentculture,
Currentcultureignorecase,
Invariantculture,
Invariantcultureignorecase,
Ordinal,
Ordinalignorecase
}
}

Maybe you haven't considered that much when comparing strings at ordinary times, " = " , " Eaqual () " It is easy to use. In fact, there are many things, such as performance and region information.
1 First, stringcomparison. ordinal is non-verbal (non-verbal) when calling string. Compare (string1, string2, stringcomparison. ordinal ). - Linguistic) comparison, the API runtime will compare the two strings at the byte level, so this comparison is more strict and accurate, and the performance is also good, generally through stringcomparison. ordinal for comparison than string. compare (string1, string2) is about 10 times faster. (You can write a simple small program for verification. This surprised me because we usually use string. compare has never thought so much ). stringcomparison. ordinalignorecase is a byte-level comparison that ignores case sensitivity. the performance is slightly weaker than that of stringcomparison. ordinal.

2 . Stringcomparison. currentculture is the default comparison method for string. Compare when no stringcomparison is specified. The example is as follows:

Thread. currentthread. currentculture =   New Cultureinfo ( " En-US " ); // The current region information is in the United States.
String S1 =   " Visual Studio " ;
String S2 =   " Windows " ;
Console. writeline (string. Compare (S1, S2, stringcomparison. currentculture )); // Output "-1"

Thread. currentthread. currentculture =   New Cultureinfo ( " SV-Se " ); // The current region information is Sweden.
Console. writeline (string. Compare (S1, S2, stringcomparison. currentculture )); // Output "1" stringcomarison. currentcultureignorecase indicates a case-insensitive comparison in the current region information.

3 . Stringcomarison. invariantculture uses stringcomarison. invariantculture is used to compare strings. In any system (different culture), the same result is obtained. It uses cultureinfo. compareinfo, a static member of invariantculture, for comparison. example:
Thread. currentthread. currentculture =   New Cultureinfo ( " En-US " ); // The current region information is in the United States.
String S1 =   " Visual Studio " ;
String S2 =   " Windows " ;
Console. writeline (string. Compare (S1, S2, stringcomparison. invariantculture )); // Output "-1"

Thread. currentthread. currentculture =   New Cultureinfo ( " SV-Se " ); // The current region information is Sweden.
Console. writeline (string. Compare (S1, S2, stringcomparison. invariantculture )); // Output "-1"

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.