如何比较不区分大小写和不区分重音的字符串
How to compare strings with case insensitive and accent insensitive
如何比较不区分大小写和不区分重音的字符串
好的,这在 SQL 服务器上很容易完成
但是我想在 C# .NET 4.5.1 上做同样的事情。
我怎样才能以最正确的方式做到这一点?
我的意思是这 3 个字符串在比较时应该 return 相等
http://www.buroteknik.com/metylan-c387c4b0ft-tarafli-bant-12cm-x25mt_154202.html
http://www.buroteknik.com/METYLAN-C387C4B0FT-TARAFLI-BANT-12cm-x25mt_154202.html
http://www.buroteknik.com/METYLAN-C387C4B0FT-TARAFLı-BANT-12cm-x25mt_154202.html
我需要一种方法来证明下面这两个是相同的SQL服务器说它们是相等的。
tarafli
TARAFLİ
您可以将 string.Compare
与采用适当 CultureInfo
和 CompareOptions
的重载一起使用:
string.Compare(s1, s2, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace |
CompareOptions.IgnoreCase);
编辑:
关于 CultureInfo
的问题,来自 MSDN:
The comparison uses the culture parameter to obtain culture-specific
information, such as casing rules and the alphabetical order of
individual characters. For example, a particular culture could specify
that certain combinations of characters be treated as a single
character, that uppercase and lowercase characters be compared in a
particular way, or that the sort order of a character depends on the
characters that precede or follow it.
要忽略大小写和重音,您可以将 string.Compare()
与 IgnoreNonSpace
和 IgnoreCase
选项一起使用,如下所示:
string s1 = "http://www.buroteknik.com/metylan-c387c4b0ft-tarafli-bant-12cm-x25mt_154202.html";
string s2 = "http://www.buroteknik.com/METYLAN-C387C4B0FT-TARAFLI-BANT-12cm-x25mt_154202.html";
string s3 = "http://www.buroteknik.com/METYLAN-C387C4B0FT-TARAFLı-BANT-12cm-x25mt_154202.html";
Console.WriteLine(string.Compare(s1, s2, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase));
Console.WriteLine(string.Compare(s2, s3, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase));
针对您在下方的评论,这也适用于 tarafli
和 TARAFLİ
。
以下代码打印 0,表示字符串相等:
string s1 = "tarafli";
string s2 = "TARAFLİ";
Console.WriteLine(string.Compare(s1, s2, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase));
这里使用的是土耳其文化(我猜正确的文化是什么)。
这也打印 0:
string s1 = "tarafli";
string s2 = "TARAFLİ";
var trlocale = CultureInfo.GetCultureInfo("tr-TR");
Console.WriteLine(string.Compare(s1, s2, trlocale, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase));
如何比较不区分大小写和不区分重音的字符串
好的,这在 SQL 服务器上很容易完成
但是我想在 C# .NET 4.5.1 上做同样的事情。
我怎样才能以最正确的方式做到这一点?
我的意思是这 3 个字符串在比较时应该 return 相等
http://www.buroteknik.com/metylan-c387c4b0ft-tarafli-bant-12cm-x25mt_154202.html
http://www.buroteknik.com/METYLAN-C387C4B0FT-TARAFLI-BANT-12cm-x25mt_154202.html
http://www.buroteknik.com/METYLAN-C387C4B0FT-TARAFLı-BANT-12cm-x25mt_154202.html
我需要一种方法来证明下面这两个是相同的SQL服务器说它们是相等的。
tarafli
TARAFLİ
您可以将 string.Compare
与采用适当 CultureInfo
和 CompareOptions
的重载一起使用:
string.Compare(s1, s2, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace |
CompareOptions.IgnoreCase);
编辑:
关于 CultureInfo
的问题,来自 MSDN:
The comparison uses the culture parameter to obtain culture-specific information, such as casing rules and the alphabetical order of individual characters. For example, a particular culture could specify that certain combinations of characters be treated as a single character, that uppercase and lowercase characters be compared in a particular way, or that the sort order of a character depends on the characters that precede or follow it.
要忽略大小写和重音,您可以将 string.Compare()
与 IgnoreNonSpace
和 IgnoreCase
选项一起使用,如下所示:
string s1 = "http://www.buroteknik.com/metylan-c387c4b0ft-tarafli-bant-12cm-x25mt_154202.html";
string s2 = "http://www.buroteknik.com/METYLAN-C387C4B0FT-TARAFLI-BANT-12cm-x25mt_154202.html";
string s3 = "http://www.buroteknik.com/METYLAN-C387C4B0FT-TARAFLı-BANT-12cm-x25mt_154202.html";
Console.WriteLine(string.Compare(s1, s2, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase));
Console.WriteLine(string.Compare(s2, s3, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase));
针对您在下方的评论,这也适用于 tarafli
和 TARAFLİ
。
以下代码打印 0,表示字符串相等:
string s1 = "tarafli";
string s2 = "TARAFLİ";
Console.WriteLine(string.Compare(s1, s2, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase));
这里使用的是土耳其文化(我猜正确的文化是什么)。 这也打印 0:
string s1 = "tarafli";
string s2 = "TARAFLİ";
var trlocale = CultureInfo.GetCultureInfo("tr-TR");
Console.WriteLine(string.Compare(s1, s2, trlocale, CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase));