C++如何检查字母isalpha(不是拉丁字母) [英] C++ How to check if letter isalpha (not latin alphabet)

查看:45
本文介绍了C++如何检查字母isalpha(不是拉丁字母)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我需要看看我的角色是不是一个字母.我尝试使用 isalpha() 函数,但是,如果我尝试传递非拉丁字母(例如 ą, č, ę, ė, į, š, ų, ū, ž)我得到一个错误,这似乎表明 isalpha函数只接受 ASCII 码中介于 0 和 255 之间的字符.有什么办法可以解决这个问题?

so I need to see if my character is a letter. I tried using isalpha() function, however, if I try to pass not latin letter (for example ą, č, ę, ė, į, š, ų, ū, ž) I get an error, that seems to state that isalpha function accepts only chars that in ASCII code are between 0 and 255. Is there any way to overcome this problem?

推荐答案

您可以使用 locale 版本的 std::isalpha.以链接参考中的示例为例:

You can use a locale version of std::isalpha. Taking an example from the linked reference:

#include <iostream>
#include <locale>
int main()
{
    const wchar_t c = L'u042f'; // cyrillic capital letter ya

    std::locale loc1("C");
    std::cout << "isalpha('Я​', C locale) returned "
               << std::boolalpha << std::isalpha(c, loc1) << '
';

    std::locale loc2("en_US.UTF8");
    std::cout << "isalpha('Я', Unicode locale) returned "
              << std::boolalpha << std::isalpha(c, loc2) << '
';
}

输出:

isalpha('Я​', C locale) returned false
isalpha('Я', Unicode locale) returned true

这篇关于C++如何检查字母isalpha(不是拉丁字母)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆