toupper/tolower + 区域设置(德语)

2024-03-25

如何将字符串(wstring)从小写字符转换为大写字符,反之亦然? 我在网上搜索发现有一个STL函数std::transform。
但到目前为止,我还没有弄清楚如何为该函数提供正确的区域设置对象,例如“德国德语”。 请问谁可以帮忙? 我的代码如下所示:

wstring strin = L"ABCÄÖÜabcäöü";
wstring str = strin;
locale loc( "Germany_german" ); // ??? how to apply this ???
std::transform( str.begin(), str.end(), str.begin(), (int(*)(int)tolower );
//result: "abcäöüabcäöü"

字符 äÖÜ 和 äöü(如 Ae、Oe、Ue)将无法正确转换。

P.S.:我不喜欢大开关sweat而且我知道 BOOST 无所不能,我更喜欢 STL 解决方案。

提前致谢
Oops


您需要以不同的方式应用它:

for(unsigned i=0;i<str.size();i++)
   str[i]=std::toupper(str[i],loc);

或者设置全局区域设置

std::locale::global(std::locale("Germany_german"));

and then

std::transform( str.begin(), str.end(), str.begin(), std::toupper );

See: http://www.cplusplus.com/reference/std/locale/tolower/ http://www.cplusplus.com/reference/std/locale/tolower/

Note 1C toupper 与 std::toupper 不同,后者接收 std::locale 作为它使用的参数char仅作为参数,而std::toupper适用于两者char and wchar_t.

Note 2 std::locale对于德语来说非常糟糕,因为它基于字符工作,无法将“ß”转换为“SS”,但它适用于大多数其他字符......

Note 3:如果你需要correct大小写转换包括处理“ß”等字符,您需要使用良好的本地化库,例如 ICU 或 Boost.Locale

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

toupper/tolower + 区域设置(德语) 的相关文章

随机推荐