天天看點

5.2、Qt::QString搜尋以及替換前言:main.cpp

前言:

1、本文操作均為在vs2015+QT5.9.5版本中執行
2、本文操作是基于Qt視窗進行使用

main.cpp

#include <QtCore/QCoreApplication>
#include <iostream>
#include <QDebug>
using namespace std;
int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);
//字元串讀取
	QString str = "xcjasd,asdasd{sa,[name],[id]},[name]},asda[name]}ssdsa";
	for (int i = 0; i < str.size(); i++)
	{
		//由于QCharRef類型無法讀取,需要轉換成ASCII碼進行讀取。
		cout << str[i].toLatin1();//“toLatin1()拉丁文”也就是ASCII編碼方式
	}
	cout << endl;
	cout << "=================================================================" << endl;
	//周遊器讀取字元串
	QString::iterator itr;//一個QString的周遊器
	for (itr = str.begin(); itr != str.end(); itr++)
	{
		//兩種方式周遊轉換,
			//第一種:指針周遊通路toLatin1()轉換
			//cout << (*itr).toLatin1();
			//第二種:周遊器指向toLatin1()轉換
		cout << itr->toLatin1();
	}
	cout << endl;
	cout << "=================================================================" << endl;
//目标通路查找
	QString key = "[name]";
	//找到第一個name的位置
	int pos = str.indexOf(key);
	cout << "pos=" << pos << endl << endl;
	//找到第二個name的位置
	int pos2 = str.indexOf(key, pos + 1);//在這裡可以使用+1進行查找
	cout << "pos2=" << pos2 << endl << endl;
	//找到第三個name的位置
	int pos3 = str.indexOf(key, pos2 + key.size());//或者采用這個方法pos2 + key.size()
	cout << "pos3=" << pos3 << endl << endl;
	//如果沒有這個值它會傳回為 -1;
	cout << str.indexOf("ttttt") << endl;
	cout << "=================================================================" << endl; 
//字元串截取
	//str.chop(5);//去掉後面五位
	//qDebug() <<QString::fromLocal8Bit("減去後五位=")<< str;
    //擷取位置字元位:
	int b_pos =str.indexOf("{");//擷取"{"位置          數值:13
	int e_pos = str.lastIndexOf("}");//擷取"}"的位置   數值:46
	
    //left(b_pos);擷取從左開始到b_pos之前數值的字元串  PS:不包括"{"本身
	qDebug() << QString::fromLocal8Bit("取大括号之前=") << str.left(b_pos) << endl;

	//right(e_pos);擷取從右邊到"}"數值的字元串   
	//PS:str.right(字元串長度 - e_pos - 減去一個"}"位置),不減去的話會包括"}"本身
	qDebug() << QString::fromLocal8Bit("取大括号之後=") << str.right(str.size() - e_pos - 1) << endl;
	
	//取中間的字元串
	QString str2 = str;
	str2.chop(str2.size() - e_pos);//減去從右邊到"}"之前的字元串
	qDebug() << QString::fromLocal8Bit("取大括号中間的字元串=") << str2.right(e_pos - b_pos-1) << endl;
	cout << "=================================================================" << endl;
//字元串替換
	QString str3 = str;
/********************************************/
//替換[name]為zzp
	str3.replace("[name]","zzp");
	//讀取操作
	cout << "[name]字元串替換後:";
	for (int i = 0; i < str3.size(); i++)
	{
		cout<< str3[i].toLatin1();
	}
	cout << endl << endl;
/********************************************/
//替換[id]為15
	str3.replace("[id]", "15");
	qDebug() << QString::fromLocal8Bit("[id]字元串替換後:") << str3 <<endl;
/********************************************/
	cout << "=================================================================" << endl;
//字元串切割
	QString str4 = str;
	//split();切割字元串函數,用法如下
	QStringList str_list = str4.split(",");
	//周遊操作
	for (int i = 0; i < str_list.size(); i++)
	{
		qDebug() << QString::fromLocal8Bit("字元串切割後=")<< str_list[i];
	}
	cout << endl;
	cout << "=================================================================" << endl;

	return a.exec();
}

           

繼續閱讀