1

How to check in C++ if a character is a letter of some alphabet? Generally I need something like this:

bool is_german(wchar_t ch);
bool is_russian(wchar_t ch);
bool is_japanese(wchar_t ch);

and etc.

EDIT 1. Can I do it without defining all charachter sets of all languages I need. Or maybe there is some library which has somethis like this:

std::vector alphabet = GetEnglishAlphabet(); // alphabet = {L'a', L'b', L'c', ...}

EDIT 2. If someone is interested in I've found

Script QChar::script() const

4

3 回答 3

2

对于自己动手的解决方案,我通常会期待这样的事情:

vector<wchar_t> german = {... german chars ...};
vector<wchar_t> japanese = {... japanese chars ...};
vector<wchar_t> russian = {... russian chars ...};

bool is_in_alphabet(const vector<wchar_t>& language, wchar_t candidate) {
   return std::find(language.begin(), language.end(), candidate) != language.end();
}
于 2014-02-14T06:16:22.007 回答
1

您可以使用std::isalpha定义在<locale>. 请记住首先设置为正确的语言环境 http://www.cplusplus.com/reference/locale/isalpha/

编辑:

std::locale loc("en-US");
bool isAlpha1 = std::isalpha('a', loc);
bool isAlpha2 = std::isalpha('&', loc);
bool isAlpha3 = std::isalpha('1', loc);
bool isAlpha4 = std::isalpha('Ж', loc); //cyrilic alphabet, but not US

您可以在此处找到语言字符串:

http://msdn.microsoft.com/en-us/library/39cwe7zf.aspx

http://msdn.microsoft.com/en-US/goglobal/bb896001.aspx

于 2014-02-14T07:41:53.797 回答
0

还有 ICU 库,具有ublock_getCode功能。但是请注意,您无法说出确切的语言,因为相同的字母用于不同的字母表。

于 2014-02-14T07:37:49.593 回答