【问题标题】:How to make languages-friendly function to lower?如何降低语言友好功能?
【发布时间】:2014-04-24 19:13:19
【问题描述】:

我希望一个函数“降低”(来自 word)在两种语言(例如英语和俄语)上正常工作。我应该怎么办?我应该使用std::wstring,还是可以使用std::string? 我也希望它是跨平台的,不要重新发明轮子。

【问题讨论】:

  • 这是一个复杂的问题。确保您了解语言环境并已阅读以下内容:joelonsoftware.com/articles/Unicode.html
  • 最后,为了让它正确,您不得不使用您选择的编码(首选 UTF-8)中的 unicode 字符串。没有为单个 unicode 代码点正确定义更改大小写(小写、大写、标题、折叠)。尽管如此,仍有许多语言对这些转换有相互矛盾的定义。
  • 所以我应该使用unicode,还有什么?我确切地知道我将拥有哪些语言。两者之一。它无法提供帮助?
  • 使用 ICU 的 boost 接口(又名 Boost.Locale)可能更简单,但您不会逃避安装 ICU。
  • @Ava_Katushka:我相信this function 与你想做的事情有关。

标签: c++ string wstring tolower


【解决方案1】:

这类东西的规范库是 ICU:

http://site.icu-project.org/

还有一个 boost 包装器:

http://www.boost.org/doc/libs/1_55_0/libs/locale/doc/html/index.html

另请参阅此问题: Is there an STL and UTF-8 friendly C++ Wrapper for ICU, or other powerful Unicode library

首先确保您了解语言环境的概念,并且牢牢掌握 Unicode 和更普遍的编码系统的含义。

一些不错的快速入门读物:

http://joelonsoftware.com/articles/Unicode.html

http://en.wikipedia.org/wiki/Locale

【讨论】:

    【解决方案2】:

    我认为这个解决方案没问题。我不确定它是否适合所有情况,但很有可能。

    #include <locale>
    #include <codecvt>
    #include <string>
    
    std::string toLowerCase (const std::string& word) {
        std::wstring_convert<std::codecvt_utf8<wchar_t> > conv;
        std::locale loc("en_US.UTF-8");
        std::wstring wword = conv.from_bytes(word);
        for (int i = 0; i < wword.length(); ++i) {
           wword[i] = std::tolower(word[i], loc);
        }
       return conv.to_bytes(wword);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-21
      • 2020-06-01
      • 2012-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-19
      • 2011-03-27
      相关资源
      最近更新 更多