在C ++中比较字符串fussballmanager和fußballmanager [英] Comparing strings fussballmanager and fußballmanager in C++

查看:77
本文介绍了在C ++中比较字符串fussballmanager和fußballmanager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SQL中,你可以根据整理成功比较两个字符串

使用Latin1_General_CI_AS,fussballmanager和fußballmanager被认为是平等的。



它们与SQL_Latin1_General_CP1_CI_AS不同。



我试图在C ++中复制相同的行为。我理解它依赖于语言环境,我希望有一些代码,用户可以决定使用哪个语言环境。



我尝试了什么:



STL



 std :: locale loc1(german ); 

wstring a(fussballmanager);
wstring b(fußballmanager);

const std :: collat​​e< wchar_t>& coll = use_facet< std :: collat​​e< wchar_t> >(LOC1);
int nRes = coll.compare(a.data(),a.data()+ a.size(),
b.data(),b.data()+ b.size() );
// => nRes是1而不是0





BOOST



 boost :: locale :: generator gen; 
std :: locale loc = gen(en-GB);
std :: locale :: global(loc);


wstring a(fussballmanager);
wstring b(fußballmanager);
int nRes = use_facet< boost :: locale :: collat​​or< wchar_t> >(loc).compare(boost :: locale :: collat​​or_base :: primary,a,b);
//崩溃在这里(如果我将区域设置字符串留空,它会起作用:用

解决方案

在这个SO线程中有一个关于字符串比较的非常好的答案:

c ++ - 如何使用任意语言环境比较basic_string - Stack Overflow [ ^ ]。



关于你的SQL例子,当使用 AS (Accent Sensitive)时,两者都应该为示例字符串返回false。但是我不太确定特殊情况 Eszett 。唯一的区别是 CP1 选择代码页1252而另一个使用默认代码页(也可能是1252)。



我没有使用过强化整理和比较f到目前为止,并没有提供使用它们的解决方案。



如果你在Windows上,你可以使用 CompareStringEx函数(Windows) [ ^ ]。



还有 ICU - Unicode的国际组件 [ ^ ]项目提供了一个支持整理比较的库(参见Collat​​ion - ICU用户指南 [ ^ ])。


In SQL, you can compare successfully two strings depending on the collation
With Latin1_General_CI_AS, fussballmanager and fußballmanager are considered equal.

They are different with SQL_Latin1_General_CP1_CI_AS.

I am trying to replicate the same behaviour in C++. I understand it's dependent on the locale and I would like to have some code where the user can decide which locale to use.

What I have tried:

STL


std::locale loc1("german");

wstring a("fussballmanager");
wstring b("fußballmanager");

const std::collate<wchar_t>& coll = use_facet<std::collate<wchar_t> >(loc1);
int nRes = coll.compare(a.data(), a.data() + a.size(),
                        b.data(), b.data()  + b.size());
// => nRes is 1 not 0



BOOST


boost::locale::generator gen;
std::locale loc = gen("en-GB");
std::locale::global(loc);


wstring a("fussballmanager");
wstring b("fußballmanager");
int nRes = use_facet<boost::locale::collator<wchar_t> >(loc).compare(boost::locale::collator_base::primary, a, b);
// Crash here (it works if I leave the locale string empty: replace en-GB by ""

解决方案

There is a really good answer about string comparison at this SO thread:
c++ - How to compare a "basic_string" using an arbitary locale - Stack Overflow[^].

Regarding your SQL examples, both should return false for the example strings when using AS (Accent Sensitive). But I'm not quite sure about the special case Eszett. The only difference is CP1 which selects code page 1252 while the other uses a default code page (which might be 1252 too).

I have not used the boost collating and comparing functions so far and can't therefore give a solution using them.

If you are on Windows, you can use the CompareStringEx function (Windows)[^].

There is also the ICU - International Components for Unicode[^] project providing a library that supports collated comparing (see Collation - ICU User Guide[^]).


这篇关于在C ++中比较字符串fussballmanager和fußballmanager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆