c++ - Unicode 字符的小写

标签 c++ unicode wchar-t latin

我正在处理一个需要从 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/

相关文章:

c++ - 为什么T 'skips'的模板参数推导是数组元素的const,而函数参数是对T的const引用?

javafx - 如何在 javaFX 中使用 Unicode 字体?

c++ - 在 C++ 中使用 Cstrings 反转字符串

c++ - 如何直接调用类静态函数

python - Matplotlib 中的重音字符

c++ - cp1251 : encoding distortion when converting from char* to wchar_t*

c++ - 比较 unicode 和 wchar_t C++ : `unable to find numeric literal operator ' operator ""F' `

c++ - 无法将 HTML 格式的 unicode(使用 wchar_t)复制到剪贴板

c++ - 固定大小的容器,旧元素将被删除

python - 在 Python 中大写非 ASCII 单词