对于前面获取字符串汉字全拼音的功能,大家应该有个了解了。现在我又综合广大网友流传的获取字符串汉字拼音首字母的功能进行了整理。介绍如下

这个功能写的稍微有点复杂 使用3个函数解决了获取字符串汉字首拼音串的问题。

代码如下:

 bool GetChineseCodeGBK(const char *pChineseCharacter, char *pGBK, const size_t nChineseCharacter = , const size_t nGBK = )
{
bool is_success = false; do
{
int len = sprintf_s(pGBK, nGBK, "%X%X", (unsigned char)pChineseCharacter[], (unsigned char)pChineseCharacter[]);
if (len<)
return is_success = false;
else
return is_success = true;
} while (false); return is_success;
}
 char GetFirstCharacter(const char *pGBK, const size_t nGBK = )
{
int nCode2 = atol(pGBK);//error
int nCode = strtol(pGBK, NULL, );//ok
int nCode3;
sscanf_s(pGBK, "%x",&nCode3); //ok
int areacode[] = { , , , , , , , , , , , , , , , , , , , , , , , , , , };
for (int i = ; i < ; i++)
{
if (areacode[i] <= nCode && nCode < areacode[i + ])
{
return (char)('A'+i);
}
}
return (char);
}
 std::string get_first_pinyin_string(const std::string &strHanzi)
{
std::string retFistPinyinString;
char retFistPinyin; if (strHanzi.empty() == true)
{
return ;
} for (size_t i = ; i < strHanzi.length(); ++i)
{
char GBK[] = { }; if (static_cast<unsigned char>(strHanzi[i]) < 0x80)
{
retFistPinyinString += strHanzi[i];
continue;
} if (GetChineseCodeGBK(&strHanzi[i], GBK))
{
retFistPinyin = GetFirstCharacter(GBK);
if ((char) != retFistPinyin)
{
retFistPinyinString += retFistPinyin;
++i;
}
}
} return retFistPinyinString;
}

下面是测试代码

 int _tmain(int argc, _TCHAR* argv[])
{ std::string str_test10("任晓霞l2;哈哈哈mu男人");
std::string str_test10_rt;
str_test10_rt = get_first_pinyin_string(str_test10); return ;
}

有兴趣的朋友可以试试啦。

关于C++对汉字拼音的处理(2)的更多相关文章

  1. C# 获取汉字拼音首字母

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精   本节探讨C#获取汉字拼音首字母的方法: 代码类东西, ...

  2. 通过pinyin4j.jar将(汉字拼音混合字符串)转化成字母首字母

    通过pinyin4j.jar将(汉字拼音混合字符串)转化成字母首字母 例如 我的中国心    ==> wdzgx 我的中国心ya ==> wdzgxya woai我的中国 ==> w ...

  3. C/C++ 获取汉字拼音

    参考文章:http://blog.csdn.net/thenile/article/details/6318521 在参考文章的基础上,去掉了代码中C++特有的语法和数据类型,用纯C语言实现了获取汉字 ...

  4. C/C++ 获取汉字拼音首字母

    #include <stdint.h> #include <stdio.h> #include <ctype.h> #include <string.h> ...

  5. MySQL数据库获取多个汉字拼音的首字母函数

    需求简介:最近的一个项目,想实现如下图所示的显示效果.很明显,如果能够获取对应的汉字词组中每个汉字的拼音首字母就可以实现了,如果是固定的几组汉字,人为的拼一下就可以 了,不过项目中有多处功能需要这个效 ...

  6. MySQL数据库获取汉字拼音的首字母函数

    需求简介:最近的一个项目,想实现如下图所示的显示效果.很明显,如果能够获取对应的汉字词组的拼音首字母就可以实现了,如果是固定的几个汉字,人为的拼一下就可以了,不过项目中有多处功能是需要这个效果的,并且 ...

  7. AutoCompleteTextView输入汉字拼音首字母实现过滤提示(支持多音字,Filterable的使用)

    AutoCompleteTextView具有输入提示的功能,但是它的这种提示不适合对股票列表的过滤,如果你玩过股票软件,就会知道只要输入股票名称的首字母或股票代码就会出现符合匹配的股票,这种过滤怎么实 ...

  8. C#获取汉字拼音

    C#获取汉字拼音 using System;  using System.Collections.Generic;  using System.Text;  using System.Text.Reg ...

  9. C# 使用微软的Visual Studio International Pack 类库提取汉字拼音首字母

    首先下载Visual Studio International Pack 1.0,官方下载地址:http://www.microsoft.com/downloads/zh-cn/details.asp ...

  10. OC1_汉字拼音转换 练习

    // // WordManager.h // OC1_汉字拼音转换 // // Created by zhangxueming on 15/4/27. // Copyright (c) 2015年 z ...

随机推荐

  1. POJ-1151 Atlantis 矩形面积并

    题目链接:http://poj.org/problem?id=1151 扫描线+离散+线段树,线段树每个节点保存的是离散后节点右边的线段. //STATUS:C++_AC_16MS_208KB #in ...

  2. MongoDB 入门之查询(find)

    MongoDB 入门之查询(find) 1. find 简介 (1)find的第一个参数决定了要返回哪些文档. 空的查询文档会匹配集合的全部内容.默认就是{}.结果将批量返回集合c中的所有文档. db ...

  3. Combobox 成员添加

    this.comboBox1.Items.AddRange(new object[] {"Item 1", "Item 2", "Item 3&quo ...

  4. PHP使用IP地址连接MySQL数据库

    我们通常测试的时候都是用mysql_connect("localhost","usrname","password");的方式来连接MySQ ...

  5. children真的没有兼容性问题吗?

    获取某一节点的所有子元素节点,你会用什么方法? 如果你用childNodes,那会有兼容性问题,对于标准浏览器,他包含的是元素节点和文本节点,我们还需要循环来判断节点的类型,是文本节点还是元素节点,不 ...

  6. AndroidAnnotations部署

    环境: 系统:windows 8 (64bit) 开发工具:Eclipse 3.8 JDK版本:jdk1.6 构建工具:Ant(Eclipse默认的build tool) androidannotat ...

  7. 【42】了解typename的双重意义

    1.在template声明中,class与typename是等价的,但是使用typename更好. 2.在template实现中,模版形参是从属名称,嵌套在模版形参中的类型是嵌套从属名称,不依赖任何t ...

  8. nginx 安装手记 分类: Nginx 服务器搭建 2015-07-14 14:28 15人阅读 评论(0) 收藏

    Nginx需要依赖下面3个包 gzip 模块需要 zlib 库 ( 下载: http://www.zlib.net/ ) zlib-1.2.8.tar.gz rewrite 模块需要 pcre 库 ( ...

  9. direct3D directX

    direct3D只是directX其中一个增强功能 DirectX是由很多API组成的,按照性质分类,可以分为四大部分,显示部分.声音部分.输入部分和网络部分. 显示部分担任图形处理的关键,分为Dir ...

  10. LINUX 系统备份

    系统备份是系统管理工作中十分重要的一个环切,本文详细介绍了各种Linux系统的备份方法,相信对大家的日常管理工作有所帮助. 备份是一项重要的工作,但是很多人没有去做.一旦由于使用不当造成数据丢失,备份 ...