- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
如何将字符串 (wstring) 从小写字符转换为大写字符,反之亦然?我在网上搜索了一下,发现有一个 STL 函数 std::transform。
但直到现在我还没有想出如何为函数提供正确的语言环境对象,例如“Germany_german”。请问谁可以帮忙?我的代码看起来像:
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 解决方案。
提前致谢
糟糕
最佳答案
您需要以不同的方式应用它:
for(unsigned i=0;i<str.size();i++)
str[i]=std::toupper(str[i],loc);
或者设置全局语言环境
std::locale::global(std::locale("Germany_german"));
然后
std::transform( str.begin(), str.end(), str.begin(), std::toupper );
参见:http://www.cplusplus.com/reference/std/locale/tolower/
注意 1 C toupper 不同于 std::toupper,std::toupper 接收 std::locale 作为参数,它仅使用 char
作为参数,而 std::toupper
适用于 char
和 wchar_t
。
注释 2 std::locale
对于德语来说很糟糕,因为它基于字符工作,不能将“ß”转换为“SS”,但它可以工作对于大多数其他角色...
注意 3:如果您需要正确大小写转换,包括处理“ß”等字符,您需要使用良好的本地化库,如 ICU 或 Boost.Locale
关于c++ - toupper/tolower + 语言环境(德语),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2598569/
使用 String.toUpper() 时尝试“格式化”不包含大写字符(例如阿拉伯语)的语言时,是否必须采取任何其他预防措施? string arabic = "مرحبا بالعالم"; str
对于以下功能 void display() { for (int i = 0; i 97 && (i+j)%2 == 0) SetConsoleTextAtt
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
这里我检查输入数组 (inputText[]) 中的每个字符是否至少与 LETTERS 数组中的一个字母匹配。但 toupper() 函数对我不起作用。我正在 Linux mint 上工作并使用 gc
我在尝试理解 toupper() 函数时面临一个有趣的问题。请找到下面的代码:这里的问题是在输出中我总是缺少输入字符串的第一个字符。 请纠正我的错误。我在谷歌搜索了太多之后才发布此内容。 void U
这个问题已经有答案了: Lowercase characters to Uppercase characters in C & writing to file (1 个回答) 已关闭 7 年前。 我正
我尝试了以下方法:*string = toupper(*string); 这仅使指针指向的字符串指针的第一个字符变为大写。我希望 *pointer 指向的所有字符指针均为大写。无论如何我可以做到这一点
我正在从服务器传递一条消息,该消息存储在一个名为 strObject 的字符串变量中。我希望将 strObject 中的字符串转换为大写。所以,我使用 ToUpper() 方法。但是,当我添加一个断点
我正在使用 substring 和 IndexOf 来定位字符串中的值,但如果下面的字符串 (strOldValue) 包含不同大小写的任何字符串,我的代码将不起作用。因此,如果 strOldValu
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: Convert a String In C++ To Upper Case 你好,我需要一个可移植的函数来将
我正在尝试将字符串转换为大写以便我可以对其进行操作,但是虽然我可以成功地操作自然大写字符串以及将小写转换为大写,但使用这种转换方法无法进行操作。 例如,如果我通过加密传递“hello”,我的加密字符串
我已经创建了一组算法,它接受一个字符串 vector 的输入,检查是否有任何字符串出现不止一次:如果是这样,从 vector 中删除所有额外出现的字符串,然后输出新的,'lighter ' 没有冗余的
我实现了 toUpper()。它并非 100% 有效。 代码: char* toUpper(char* string) { char* sv = string; while(*sv++
我很好奇 .ToUpper() 是如何工作的?是否存在某种映射,较低的 a 具有 UTF 代码 XYZ,而较高的 a 具有 UTF 代码 XYZ1? 最佳答案 是的,它正在使用 Unicode 元数据
我有一个 []byte,b,我想选择一个字节,b[pos] 并改变它大写(然后是小写) bytes 类型有一个名为 ToUpper() 的方法。如何将它用于单个字节? 在单个字节上调用 ToUpper
我在使用 toupper() 函数时遇到问题: 代码: #include #include using namespace std; int main (){ string input {
如果 ToUpper() 不存在,你会怎么写? i18n 和 L10n 的奖励积分 由此引发的好奇心:http://thedailywtf.com/Articles/The-Long-Way-toUp
我见过以下使用自定义 toupper() 函数的程序。 #include void my_toUpper(char* str, int index) { *(str + index) &=
长期潜伏者需要帮助,但我无法通过搜索找到。好吧,我在撒谎,我发现了一些东西,但它对我没有帮助。 我想做的是创建一个包含字符串的 char 数组并显示,将它们传递给一个函数,该函数将大写字母字符(应该只
我被分配重新编码 toUpper 和 toLower 函数。我是否必须在 if 语句中写出所有 26 个字母,例如: if ( char == 'a' ) return 'A'; 或者还有其他简单的方
我是一名优秀的程序员,十分优秀!