Nim*_*oud 5 c++ string-comparison
大家好!
在土耳其语中,其中一个字母表有不同的行为,它是我和我.在英语中我和我是大写和小写.在土耳其小写的我不是我,而是ı.
所以在土耳其环境(即Windows)中,"DOMAIN.COM"和"domain.com"不相等.由于电子邮件传输和DNS完全使用英语,如果邮件地址包含大写字母I,则可能存在问题.
在C#中,我们可以使用InvariantCultureIgnoreCase标志来纠正问题:
// Mock
string localDomain = "domain.com";
string mailAddress = "USER@DOMAIN.COM";
string domainOfAddress = mailAddress.Split('@')[1];
string localInbox = "";
//
// Local inbox check
//Case insensitive method
bool ignoreCase = true; // Equal to StringComparison.CurrentCultureIgnoreCase
if (System.String.Compare(localDomain, domainOfAddress, ignoreCase) == 0)
{
// This one fails in Turkish environment
localInbox = mailAddress.Split('@')[0];
}
//Culture-safe version
if (System.String.Compare(localDomain, domainOfAddress, StringComparison.InvariantCultureIgnoreCase) == 0)
{
// This one is the correct/universal method
localInbox = mailAddress.Split('@')[0];
}
Run Code Online (Sandbox Code Playgroud)
由于我没有C++经验,这两个例子的C++等价物是什么?
如果您正在编程Windows,则可以locale将线程更改为en_US然后使用_stricmp,或者为其创建语言环境对象en_US,然后使用_stricmp_l:
setlocale( LC_CTYPE, "English" );
assert( _stricmp("DOMAIN", "domain") == 0 );
_locale_t l = _create_locale( LC_CTYPE, "English" );
assert( _stricmp_l("DOMAIN", "domain", l) == 0 );
Run Code Online (Sandbox Code Playgroud)
如果boost是您的选择,更便携和C++友好的解决方案是使用boost :: locale库