简体   繁体   English

toupper / tolower + locale(德语)

[英]toupper/tolower + locale (german)

how to convert a string (wstring) from lowercase to uppercase characters and vice versa? 如何将字符串(wstring)从小写字符转换为大写字符,反之亦然? I searched the net and found there is a STL-function std::transform. 我在网上搜索,发现有一个STL函数std :: transform。
But until now I hav'nt figured out how to give the right locale-object for example "Germany_german" to the function. 但到目前为止,我还没想出如何将正确的语言环境对象(例如“Germany_german”)赋予该函数。 Who can help please? 谁能帮帮忙? my code looks like: 我的代码看起来像:

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äöü"

The characters ÄÖÜ and äöü (it's like Ae, Oe, Ue) will not be converted correctly. 角色ÄÖÜ和äöü(就像Ae,Oe,Ue)将无法正确转换。

PS: I don't prefer a big switch sweat and also I know BOOST is capable of everything, i would prefer a STL solution. PS:我不喜欢大开关 ,而且我也知道BOOST能胜任一切,我更喜欢STL解决方案。

thanks in advance 提前致谢
Oops 哎呀

You need to apply it in different way: 您需要以不同的方式应用它:

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

Or set global locale 或者设置全局区域设置

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 1 C toupper is different from std::toupper which receives std::locale as parameter it uses char only as parameter while std::toupper works on both char and wchar_t . 注1 C toupper不同于std :: toupper,它接收std :: locale作为参数,它只使用char作为参数,而std::toupper同时适用于charwchar_t

Note 2 std::locale is quite broken for German because it works on character basis and can't convert "ß" to "SS" but it would work for most of other characters... 注意2 std::locale对于德语非常破碎,因为它在字符基础上工作,不能将“ß”转换为“SS”,但它适用于大多数其他字符......

Note 3: If you need correct case conversion including handling characters like "ß", you need use good localization library like ICU, or Boost.Locale 注3:如果你需要正确的大小写转换,包括处理像“ß”这样的字符,你需要使用像ICU或Boost.Locale这样的好的本地化库。

The problem is that tolower() / toupper() are fundamentally broken for many locales. 问题是tolower() / toupper()从根本上打破了许多语言环境。 How do you propose toupper() should work with words containing an 'ß' ? 你如何建议toupper()应该使用包含'ß'单词? What would be the all-upper-case of "Maße" ?[1] There are similar examples in French and other languages. 什么是"Maße"的全部案例?[1]在法语和其他语言中也有类似的例子。

So the first thing to ask yourself is why you need this and what you want to achieve. 所以首先要问自己,为什么你需要这个以及你想要实现的目标。

If you're willing to ignore all these problems, here is the first treatment on the topic google found me. 如果你愿意忽略所有这些问题, 是谷歌找到我的第一个主题。 (If you're going to google for more of this, James Kanze has over the years posted many many useful insights into this problem in quite a few of the C++ newsgroups.) (如果你想要谷歌更多的话,詹姆斯坎泽多年来在很多C ++新闻组中发布了很多关于这个问题的有用见解。)

[1] A hint for the few non-Germans hanging out here :) : In German, the "ß", literally a ligature of "sz", is not available as a capital letter. [1]暗示少数非德国人在这里闲逛:) :在德语中,“ß”,字面意思是“sz”的结扎,不能作为大写字母。 It can be replaced by "ss", but not always. 它可以被“ss”取代,但并非总是如此。 For example, "Masse" is a completely different word than "Maße". 例如,“Masse”与“Maße”完全不同。

您是否尝试使用"de_DE.UTF-8"进行区域设置构建?

If you're on Unix, see /usr/share/locale/ for available locales. 如果你在Unix上,请参阅/usr/share/locale/获取可用的语言环境。 It looks like you want "de_DE.UTF-8" . 看起来你想要"de_DE.UTF-8"

But setlocale(LC_ALL, ""); 但是setlocale(LC_ALL, ""); should set the program to work in the system's locale, whatever it is. 应该将程序设置为在系统的语言环境中工作,无论它是什么。

Your program works for me (I fixed a close-paren) if I simply set the default locale like that, without specifying Germany. 如果我只是设置这样的默认语言环境,而不指定德语,那么你的程序适合我(我修了一个关闭)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++下/上字符char指针 - C++ tolower/toupper char pointer 如何在字符串数组上使用 toupper 或 tolower 函数 - How can I use the toupper or tolower function on a string array isupper(),islower(),toupper(),tolower()函数在c ++中不起作用 - the isupper(), islower(), toupper(), tolower() functions not working in c++ toupper() 和 tolower() 在应该是字符时返回 ASCII 数字 - toupper() and tolower() return ASCII number when should be character 不使用toUpper(),toLower(),length(),at()或[] —转换为“嘿! 那里!”到“嗨! 那里!” - Without using toUpper(), toLower(), length(), at() or [] — Convert “Hey!! THERE!” to “hEy!! tHeRe!” 我是否需要在调用 toupper()、tolower() 等之前转换为 unsigned char? - Do I need to cast to unsigned char before calling toupper(), tolower(), et al.? 如何使用德语语言环境在gdb中设置双变量? - How to set a double variable in gdb with German locale? 当系统区域设置为法语时,仅从资源dll加载德语资源 - German resources loaded from resource only dll when system locale is french 使用嵌入在VT_BSTR中的小数点和德语语言环境时,转换不正确 - Incorrect conversion when decimal point embedded in VT_BSTR and German locale used C ++ toUpper Implementation - C++ toUpper Implementation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM