字符的Unicode编码、单个字符的判断
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
// 每个字符的Unicode
void Widget::on_btnGetChars_clicked()
{
// 获取输入框信息
QString str = ui->editStr->text();
if(str.isEmpty()) return;
for(qint16 i = 0; i < str.size(); i++){
QChar ch = str.at(i);
char16_t uniCode = ch.unicode();
QString chStr(ch);
QString info = chStr + QString::asprintf("\t, Unicode编码=0x%X", uniCode);
ui->plainTextEdit->appendPlainText(info);
}
}
// 清空文本框
void Widget::on_btnClear_clicked()
{
ui->plainTextEdit->clear();
}
// 每个字符特性判断
void Widget::on_btnCharJudge_clicked()
{
QString str = ui->editChar->text();
if(str.isEmpty()){
return;
}
QChar ch = str.at(0);
char16_t uniCode = ch.unicode();
QString info = str + QString::asprintf("\t, Unicode编码=0x%X", uniCode);
ui->plainTextEdit->appendPlainText(info);
ui->checkDigit->setChecked(ch.isDigit()); // 是否为数字 0~9
ui->checkLetter->setChecked(ch.isLetter()); // 是否为字母
ui->checkLetterOrNum->setChecked(ch.isLetterOrNumber()); // 是否位数字或字母
ui->checkUpper->setChecked(ch.isUpper()); // 是否为大写字母
ui->checkLower->setChecked(ch.isLower()); // 是否为小写字母
ui->checkMark->setChecked(ch.isMark()); // 是否为字符
ui->checkSpace->setChecked(ch.isSpace()); // 是否为空白字符
ui->checkSymbol->setChecked(ch.isSymbol()); // 是否为符号
ui->checkPunct->setChecked(ch.isPunct()); // 是否为标点符号
}
// 与Latin1的转换
void Widget::on_btnCvtLatin_clicked()
{
QString str = "Dimple";
ui->plainTextEdit->appendPlainText(str);
QChar ch = QChar('P');
str[0] = ch;
ui->plainTextEdit->appendPlainText("\n"+str);
}
// 与UTF-16的转换
void Widget::on_btnUtfCvt_clicked()
{
QString str = "Hello,北京";
ui->plainTextEdit->appendPlainText(str);
QString cd = "成都";
str[6] = cd.at(0);
str[7] = cd.at(1);
ui->plainTextEdit->appendPlainText("\n" + str);
}
// QChar比较和替换
void Widget::on_btnCharCmpCvt_clicked()
{
QString HuStr = "河to湖";
QChar He = QChar::fromUcs2(HuStr[0].unicode());
QChar Hu = QChar(HuStr[3].unicode());
QString str = "他们来自河南或河北";
ui->plainTextEdit->appendPlainText(str);
for(int i = 0; i < str.size(); i++){
if(str[i] == He){
str[i] = Hu;
}
}
ui->plainTextEdit->appendPlainText("\n"+str);
}