Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double-byte string comparison in C#

I have two strings one with a double-byte value and the other is a single byte-one. The string comparison result returns false, how do I get them to compare correctly after ignoring the single-byte/double-byte difference?

string s1 = "smatsumoto11"
string s2 = "smatsumoto11"

In the same scenario, if you have a nvarchar column in SQL server which contains the value smatsumoto11, a query to fetch the data with the where condition having the string smatsumoto11 will return the same row. I need similar semantics with C# string comparison.

I have tried a few options mentioned on MSDN but they don't seem to work.

Any ideas?

like image 889
agp Avatar asked Dec 22 '22 21:12

agp


1 Answers

Your s1 contains so-called "fullwidth" characters, so you can use string.Compare and tell it to ignore character width:

string.Compare(s1, s2, CultureInfo.CurrentCulture, CompareOptions.IgnoreWidth);

(Of course, specify a different CultureInfo if necessary.)

like image 116
Arnout Avatar answered Dec 24 '22 09:12

Arnout