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.