. Net (C #): Ignore symbols in string comparison

Source: Internet
Author: User

For example, there are two strings:

VaRA= "A-B = C12";

VaRB= "A. B ___ C12";

Well, obviously, no matter which common method you use, their comparison result is definitely not 0, because they are not of the same length, even if they are of the same length, they cannot be equal because of the symbols between characters.

 

How to ignore symbols in string comparison? In fact, the compareoptions. ignoresymbols enumeration option can meet this requirement. Use compareinfo in cultureinfo, and then use the compareinfo compare method to compare two strings (compareoptions enumeration can be specified here ).

 

Code:

VaRA= "A-BC = 12";

VaRB= "A. B ___ C12";

 

Console.Writeline (String.Compare (a, B ));

 

// Use invariantculture

Console.Writeline (Cultureinfo.Invariantculture.Compareinfo.Compare (A, B,Compareoptions.Ignoresymbols ));

// Current cultureinfo

Console.Writeline (Cultureinfo.Currentculture.Compareinfo.Compare (A, B,Compareoptions.Ignoresymbols ));

 

Output:

1

0

0

 

The following uses the compareoptions. ignoresymbols method to return 0.

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.