qt 获取汉字拼音首字母
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QTextCodec>
QString getChineseSpell(QString& src);
QString FirstLetter(int nCode);
char convert(int n);
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QString str = tr("这是一个正确的例子");
QString character = getChineseSpell(str);
qDebug()<<character;
}
MainWindow::~MainWindow()
{
delete ui;
}
QString getChineseSpell(QString& src)
{
QTextCodec *codec4gbk = QTextCodec::codecForName("GBK"); //获取qt提供的gbk的解码器
QByteArray buf = codec4gbk->fromUnicode(src); //qt用的unicode,转成gbk
int size = buf.size();
quint16 *array = new quint16[size+1];
QString alphbats;
for( int i = 0, j = 0; i < buf.size(); i++, j++ )
{
if( (quint8)buf[i] < 0x80 ) //gbk的第一个字节都大于0x81,所以小于0x80的都是符号,字母,数字etc
continue;
array[j] = (((quint8)buf[i]) << 8) + (quint8)buf[i+1]; //计算gbk编码
i++;
alphbats.append( convert( array[j] ) ); //相当于查表,用gbk编码得到首拼音字母
}
delete [] array;
return alphbats;
}
bool In(wchar_t start, wchar_t end, wchar_t code)
{
if (code >= start && code <= end)
{
return true;
}
return false;
}
char convert(int n)
{
if (In(0xB0A1,0xB0C4,n)) return 'a';
if (In(0XB0C5,0XB2C0,n)) return 'b';
if (In(0xB2C1,0xB4ED,n)) return 'c';
if (In(0xB4EE,0xB6E9,n)) return 'd';
if (In(0xB6EA,0xB7A1,n)) return 'e';
if (In(0xB7A2,0xB8c0,n)) return 'f';
if (In(0xB8C1,0xB9FD,n)) return 'g';
if (In(0xB9FE,0xBBF6,n)) return 'h';
if (In(0xBBF7,0xBFA5,n)) return 'j';
if (In(0xBFA6,0xC0AB,n)) return 'k';
if (In(0xC0AC,0xC2E7,n)) return 'l';
if (In(0xC2E8,0xC4C2,n)) return 'm';
if (In(0xC4C3,0xC5B5,n)) return 'n';
if (In(0xC5B6,0xC5BD,n)) return 'o';
if (In(0xC5BE,0xC6D9,n)) return 'p';
if (In(0xC6DA,0xC8BA,n)) return 'q';
if (In(0xC8BB,0xC8F5,n)) return 'r';
if (In(0xC8F6,0xCBF0,n)) return 's';
if (In(0xCBFA,0xCDD9,n)) return 't';
if (In(0xCDDA,0xCEF3,n)) return 'w';
if (In(0xCEF4,0xD188,n)) return 'x';
if (In(0xD1B9,0xD4D0,n)) return 'y';
if (In(0xD4D1,0xD7F9,n)) return 'z';
return '\0';
}
qt 获取汉字拼音首字母的更多相关文章
- C# 获取汉字拼音首字母
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 本节探讨C#获取汉字拼音首字母的方法: 代码类东西, ...
- C# 获取汉字拼音首字母/全拼
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 本节探讨C#获取汉字拼音首字母的方法: 代码类东西, ...
- java获取汉字拼音首字母 --转载
在项目中要更能根据某些查询条件(比如姓名)的首字母作为条件进行查询,比如查一个叫"李晓明"的人,可以输入'lxm'.写了一个工具类如下: import java.io.Unsupp ...
- JAVA获取汉字拼音首字母
package com.common.util; import java.io.UnsupportedEncodingException; /** * 取得给定汉字串的首字母串,即声母串 * Titl ...
- php获取汉字拼音首字母的方法
现实中我们经常看到这样的说明,排名不分先后,按姓名首字母进行排序.这是中国人大多数使用的排序方法.那么在php程序中该如何操作呢? 下面就分享一下在php程序中获取汉字拼音的首字母的方法,在网上搜到的 ...
- C/C++ 获取汉字拼音首字母
#include <stdint.h> #include <stdio.h> #include <ctype.h> #include <string.h> ...
- php 获取汉字拼音首字母的函数
function getFirstChar($string){ if($string{0}>="A" and $string{0}<="z" )re ...
- 获取汉字拼音&首字母
pinyin4j https://www.cnblogs.com/yjq520/p/7681537.html
- PHP获取汉字拼音首字母 截取中文字符串
http://blog.csdn.net/everything1209/article/details/39005785 substr是按字符分割,而mb_strcut是按字节来分割,但是都不会产生半 ...
随机推荐
- Appium如何查看webview上元素
现在大部分app都是混合式的native+webview,对应native上的元素通过uiautomatorviewer很容易定位到,webview上的元素就无法识别了: 那么如何定位webview上 ...
- MVC开发中自定义返回类型
在做项目web的MVC中,会用到返回值的问题,我们一般使用AjaxResult的返回值,根据自己的需要进行自定义,如下参考: using System; using System.Collection ...
- QTAction Editor的简单使用(简洁明了)
1. 打开UI界面,选择如下图的模式 2. 添加资源名称并选择相应的资源,点击OK 3. 相应的资源就建立好了 4. 添加好的资源可以直接拖到MainWindow中
- 201671010446姚良实验十四团队项目评审&课程总结
实验十四 团队项目评审&课程学习总结 项目 内容 这个作业属于哪个课程 http://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cn ...
- python应用-输入分数 输出最高分数对应的名字
def main(): names = ['刘备', '张飞', '曹操', '袁绍', '关羽', '赵云', '周瑜'] scores=[] num=0 m=0 for name in names ...
- Does Swift support aspect oriented programming?
The foundation of Aspect Oriented Programming is the intercept pattern. We start with a crosscutting ...
- [React] Handle React Suspense Errors with an Error Boundary
Error Boundaries are the way you handle errors with React, and Suspense embraces this completely. Le ...
- python 当前时间多加一天、一小时、一分钟(转载)
首先看下,datetime的使用 import datetime >>> print datetime.datetime.now() 2017-07-15 15:01:24.6190 ...
- Numpy | 13 位运算
NumPy "bitwise_" 开头的函数是位运算函数.本章都是按二进制来操作的. NumPy 位运算包括以下几个函数: 函数 描述 bitwise_and 对数组元素执行位与操 ...
- javascript冒泡排序 至少比较N(N-1)/2次;
<script type="text/javascript"> function get(){ var num = [10,5,2,1,3,6,4,7,9,8]; va ...