How to write code to compare Japanese strings in C#?
We have an Excel file with the 'Company' document property set in Japanese, and we have code that reads the 'Company' document property and stores it as a string.
However, we do not know how to compare it against a reference/benchmark Japanese string to see if it matches.
Two questions, please:
1) What code is needed to do this comparison?
Right now, I'm using the String Comparer solution here: Compare strings with non-English characters?
var swedishComparer = StringComparer.Create(new CultureInfo("sv-Se"), true);
consultants = consultants.Where(x => x.Descri开发者_如何学Goption.Contains(vm.Description, swedishComparer)).ToList();
2) How would I store the reference/benchmark Japanese string?
That is, what should I use for 'ReferenceStringInJapanese' here:
japaneseComparer.Compare(companyName, 'ReferenceStringInJapanese') == 0
To compare the two strings, you can use a StringComparer, as you already discovered. Use it with a ja-JP CultureInfo for Japanese input:
var comparer = StringComparer.Create(new CultureInfo("ja-JP"), true);
The string to compare against can simply be placed in your source code. C# supports Unicode.
bool areEqual = comparer.Equals(input, "こんにちは");
The Visual Studio editor supports Unicode, so you should be able to paste strings of Japanese text directly into the editor.
精彩评论