我正在处理一个需要从 unicode 文本
中获取数据的 C++
项目。
我遇到一个问题,我无法降低某些 unicode 字符
。
我使用 wchar_t
来存储从 unicode 文件读取的 unicode 字符。之后,我使用 _wcslwr
来降低 wchar_t
字符串。还有很多情况仍然不低如:
Đ Â Ă Ê Ô Ơ Ư Ấ Ắ Ế Ố Ớ Ứ Ầ Ằ Ề Ồ Ờ Ừ Ậ Ặ Ệ Ộ Ợ Ự
哪个小写是:
đ â ă ê ô ơ ư ấ ắ ế ố ớ ứ ầ ằ ề ồ ờ ừ ậ ặ ệ ộ ợ ự
我尝试了 tolower
但它仍然无法正常工作。
最佳答案
如果你只调用tolower
,它会从header clocale
调用std::tolower
,这会调用tolower
仅用于 ansi 字符。
正确的签名应该是:
template< class charT >
charT tolower( charT ch, const locale& loc );
下面是两个运行良好的版本:
#include <iostream>
#include <cwctype>
#include <clocale>
#include <algorithm>
#include <locale>
int main() {
std::setlocale(LC_ALL, "");
std::wstring data = L"Đ Â Ă Ê Ô Ơ Ư Ấ Ắ Ế Ố Ớ Ứ Ầ Ằ Ề Ồ Ờ Ừ Ậ Ặ Ệ Ộ Ợ Ự";
std::wcout << data << std::endl;
// C std::towlower
for(auto c: data)
{
std::wcout << static_cast<wchar_t>(std::towlower(c));
}
std::wcout << std::endl;
// C++ std::tolower(charT, std::locale)
std::locale loc("");
for(auto c: data)
{
// This is recommended
std::wcout << std::tolower(c, loc);
}
std::wcout << std::endl;
return 0;
}
引用:
关于c++ - Unicode 字符的小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34433380/