c++11 标准模板(STL)本地化库 - std::isalpha(std::locale) 检查字符是否被本地环境分类为字母

本地化库

本地环境设施包含字符分类和字符串校对、数值、货币及日期/时间格式化和分析,以及消息取得的国际化支持。本地环境设置控制流 I/O 、正则表达式库和 C++ 标准库的其他组件的行为。

检查字符是否被本地环境分类为字母

std::isalpha(std::locale)

template< class charT >
bool isalpha( charT ch, const locale& loc );

检查给定的字符是否为给定 locale 的 std::ctype 平面分类为字母字符。

参数

ch-字符
loc-本地环境

返回值

若字符被分类为字母则返回 true ,否则返回 false 。

可能的实现

template< class charT >
bool isalpha( charT ch, const std::locale& loc ) {
    return std::use_facet<std::ctype<charT>>(loc).is(std::ctype_base::alpha, ch);
}

调用示例

#include <iostream>
#include <locale>

void try_with(wchar_t c, const char* locale)
{
    //检查给定字符是否为给定 locale 的 std::ctype 平面分类为大写字母字符。
    std::cout << "isalpha('"
              << c
              << "', locale(\""
              << locale
              << "\")) returned "
              << std::boolalpha
              << std::isalpha(c, std::locale(locale))
              << std::endl;
}

int main()
{
    //检查给定的字符在当前的 C 本地环境中是否是空白字符。
    //空格 (0x20, ' ')
    //换页(0x0c, '\f')
    //换行(0x0a, '\n')
    //回车(0x0d, '\r')
    //水平制表符(0x09, '\t')
    //垂直制表符(0x0b, '\v')

    std::cout << std::boolalpha;

    std::locale locale1;  // locale1 是经典 "C" 本地环境的副本
    std::cout << "locale1.name():   " << locale1.name() << std::endl;

    std::locale locale2("Chinese (Simplified)_China.936"); // locale2 为本地环境
    std::cout << "locale2.name():   " << locale2.name() << std::endl;

    const wchar_t isalphaC = L'\u042f'; // 西里尔大写字母 ya
    try_with(isalphaC, locale1.name().c_str());
    try_with(isalphaC, locale2.name().c_str());
    std::cout << std::endl;

    const wchar_t isalphaC2 = ' '; // ASCII ' '
    try_with(isalphaC2, locale1.name().c_str());
    try_with(isalphaC2, locale2.name().c_str());
    std::cout << std::endl;

    const wchar_t isalphaC3 = 'a'; // ASCII a
    try_with(isalphaC3, locale1.name().c_str());
    try_with(isalphaC3, locale2.name().c_str());

    return 0;
}

输出

locale1.name():   C
locale2.name():   Chinese (Simplified)_China.936
isalpha('1071', locale("C")) returned true
isalpha('1071', locale("Chinese (Simplified)_China.936")) returned true

isalpha('32', locale("C")) returned false
isalpha('32', locale("Chinese (Simplified)_China.936")) returned false

isalpha('97', locale("C")) returned true
isalpha('97', locale("Chinese (Simplified)_China.936")) returned true

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值