C++QT5跨平台界面编程原理--QString字符串处理及中文乱码问题处理

 

gbk_utf_ansi_asc字符集分析字节序

字符集

●ASCII的7位字符集128个字符

●标准ASCII中最高位(b7)用作奇偶校验1个数

●IS0-8859-1 扩展ASCII 128-255拉丁

ANSI标准

●AmericanNational Standards Institute美国国家标准学会
●多字节字符集(MBCS , Multi- ByteChactacter Set)
0到127之间的字符,依旧是1个字节代表1个字符
●(超出部分)2个字节来表示1个字符

GB2312 GBK编码
●ANSI编码6763常用汉字
●两个大于127的字符表示一个汉字
●GBK编码GB2312的扩展汉字21003个

UTF-8

●变长的编码方式
●单字节与ASCII码相同
●对于n字节的符号( n>1 )首字节前n位为1,n+ 1为0,后面字节前两位都为10

UTF-16 UTF-32



字节序BOM
●LE ( littleendian):小字节字节序低位在前
●0x001A23    低地址23 1A 00高地址
●BE ( big endian):大字节字节序
●BOM字节序标志头
●文本头FE FF是BE               FF FE是LE

QString项目创建空和NULL判断

QString

●16-bit QChars ushort Unicode 4.0
封装了字符串处理功能
●空判断==' 'isNull isEmpty
●字符串拼接+=
●格式化字符串%1 %2 arg()

#include <QCoreApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QString str;
    if(str.isNull())   //判断是否为空
    {
        qDebug()<<"isNull"<<endl;
    }
    if(str.isEmpty())
    {
        qDebug()<<"isEmpty"<<endl;
    }
    str = "";
    if(str.isNull())
    {
        qDebug()<<"isNull2"<<endl;
    }
    if(str.isEmpty())
    {
        qDebug()<<"isEmpty2"<<endl;
    }
    QString str2;
    if(str2 == "")
    {
        qDebug()<<"str2 isNULL"<<endl;
    }

    QString str3 = "test1";    //拼接
    str3 += "test2";
    str3 += 'A';
    str3.append("test3");
    qDebug()<<str3;

    QString str4("test4");
    qDebug()<<str4;

    return a.exec();
}

QString格式化字符串转换

#include <QCoreApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QString str;
    str = QString("name=%1 arg=%2 %3 %4 %5")  //字符串拼接
            .arg("xiaoming")
            .arg(15)
            .arg(14.5)
            .arg(123,0,2)   //数,宽度,2进制
            .arg(255,6,16)  //数,宽度,16进制
            ;
    qDebug()<<str;

    QString num = QString::number(14); //数转字符串
    qDebug()<<num;

    int i = num.toInt();    //字符串转整数
    qDebug()<<"i="<<i;
    qDebug()<<"num double"<<num.toDouble();

    QString num2 = QString::number(15.6);
    qDebug()<<num2;
    qDebug()<<num2.toInt();
    qDebug()<<num2.toDouble();

    return a.exec();
}

遍历字符串

#include <QCoreApplication>
#include <QDebug>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QString str = "xcjasd,asdasd{,[name],[id]},[name]},asda[name]}sdsa";

    ///
    //字符串遍历
    for(int i =0; i < str.size(); i++)
    {
        cout<<str[i].toLatin1();
    }
    cout<<endl;
    cout<<"========================\n";
    QString::iterator itr = str.begin();
    for(;itr!= str.end();itr++)
    {
        //(*itr).toLatin1();
        cout<<itr->toLatin1();
    }
    cout<<endl;

    ///
    //字符串查找
    QString key = "[name]";
    int pos = str.indexOf(key);
    cout<<"pos = "<<pos<<endl;
    int pos2 = str.indexOf(key,pos+key.size());//在某个位置之后,再查找
    cout<<"pos2 = "<<pos2<<endl;
    int pos3 = str.indexOf(key,pos2+key.size());
    cout<<"pos3 = "<<pos3<<endl;
    cout<<str.indexOf("tttt");  //找不到返回-1
    ///
    //字符串截取	
	
    //str.chop(2);//去掉字符串最后两个字符
    qDebug()<<str;

    //取第一个{括号之前  012{56}9
    int bpos = str.indexOf("{");
    int epos = str.lastIndexOf("}");
    qDebug()<<str.left(bpos);    //之前的几个字符
    //取最后一个}括号之后123456789
    qDebug()<<str.right(str.size()-epos-1);//之后的几个字符 
    //取括号之间
    QString str2 = str;
    str2.chop(str.size()-epos);  //截取
    qDebug()<<str2;
    qDebug()<<str2.right(epos-bpos-1);

    ///
    //字符串替换
    str.replace("[name]","xiaoming");
    str.replace("[id]","007");
    qDebug()<<str;


    ///
    //字符串切割 , csv
    QStringList list1 = str.split(",");
    cout<<"---------------------"<<endl;
    for(int i = 0; i<list1.size();i++)
    {
        qDebug()<<list1[i];
    }

    return a.exec();
}#include <QCoreApplication>
#include <QDebug>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QString str = "xcjasd,asdasd{,[name],[id]},[name]},asda[name]}sdsa";

    ///
    //字符串遍历
    for(int i =0; i < str.size(); i++)
    {
        cout<<str[i].toLatin1();
    }
    cout<<endl;
    cout<<"========================\n";
    QString::iterator itr = str.begin();
    for(;itr!= str.end();itr++)
    {
        //(*itr).toLatin1();
        cout<<itr->toLatin1();
    }
    cout<<endl;

    ///
    //字符串查找
    QString key = "[name]";
    int pos = str.indexOf(key);
    cout<<"pos = "<<pos<<endl;
    int pos2 = str.indexOf(key,pos+key.size());//在某个位置之后,再查找
    cout<<"pos2 = "<<pos2<<endl;
    int pos3 = str.indexOf(key,pos2+key.size());
    cout<<"pos3 = "<<pos3<<endl;
    cout<<str.indexOf("tttt");  //找不到返回-1
    ///
    //字符串截取	
	
    //str.chop(2);//去掉字符串最后两个字符
    qDebug()<<str;

    //取第一个{括号之前  012{56}9
    int bpos = str.indexOf("{");
    int epos = str.lastIndexOf("}");
    qDebug()<<str.left(bpos);    //之前的几个字符
    //取最后一个}括号之后123456789
    qDebug()<<str.right(str.size()-epos-1);//之后的几个字符 
    //取括号之间
    QString str2 = str;
    str2.chop(str.size()-epos);  //截取
    qDebug()<<str2;
    qDebug()<<str2.right(epos-bpos-1);

    ///
    //字符串替换
    str.replace("[name]","xiaoming");
    str.replace("[id]","007");
    qDebug()<<str;


    ///
    //字符串切割 , csv
    QStringList list1 = str.split(",");
    cout<<"---------------------"<<endl;
    for(int i = 0; i<list1.size();i++)
    {
        qDebug()<<list1[i];
    }

    return a.exec();
}

QString正则表达式

#include <QCoreApplication>
#include <QDebug>
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QString str = "xcxjxcxc123asdasdas 345 asdasd";
    int pos = str.indexOf(QRegExp("[0-9]+"));
    qDebug()<<pos<<endl;
    QString str2 = str;
    str2.replace(QRegExp("[0-9]+") ,"[num]");
    qDebug()<<str2<<endl;
    QStringList rlist = str.split(QRegExp("[0-9]+"));

    for(int i = 0; i < rlist.size(); i++)
    {
        qDebug()<<rlist[i];
    }
    return a.exec();
}

Qt中文乱码问题在vs项目中显示中文 

1:在vs下源码是gb2312的所以要转换。 

	QApplication a(argc, argv);
	QString str = QStringLiteral("中文测试");    //QString 内部是utf-16   参数是utf-8  所以 
                                                 // QStringLiteral转成utf-8
	QMessageBox::information(0, "title", str);

 

2.解决办法

vs:文件-》高级保存-》

 

或者::::::这样

3.

#include "vs_cn_codec.h"
#include <QtWidgets/QApplication>
#include <QDebug>
#include <QMessageBox>
#include <iostream>
#include <QTextCodec>
#include <windows.h>
using namespace std;

int main(int argc, char *argv[])
{ 	
	char *src = "元数据中文GBK";
	//元数据是gbk或者gb2312 多字节存入QString
	//本地编码方式 默认GBK
	QString str1 = QString::fromLocal8Bit(src);  //本地的转成utf-8
	qDebug() << "str1 = " << str1;
	
	//把QString 转为gbk
	cout << str1.toLocal8Bit().toStdString() << endl;  //cout用的是gbk 
	//本地处理编码方式,默认是GBK,改为UTF-8
	//QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));

	//win api调用qstring作为参数
	MessageBox(0, str2.toStdWString().c_str(), L"中文标题",0);  //宽字符

	//vs_cn_codec w;
	//w.show();
	return a.exec();
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值