STL|QTL工作笔记-QString与std::string寻找子串的区别与联系

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq78442761/article/details/84839711

目录

 

STL中的string

QTL中的QString

总结


 

STL中的string

在STL中,是采用这个逻辑进行的:

1.设置一个标志接收find的返回值;

2.当标志为XXX时,则为找到,否则为不找到;

例子如下:

#include <string>
#include <iostream>
using namespace std;

void main(){

	string testStr = "1234567ABC !@#$%^&";
	string::size_type idx;
	idx = testStr.find("3456");

	if(idx != string::npos){

		cout << "find it!" << endl;
	}

	idx = testStr.find("33333");
	if(idx == string::npos){

		cout << "Couldn't find it!" << endl;
	}



	getchar();
}

运行截图如下:

QTL中的QString

在QTL中是采用这样的逻辑进行的:

直接采用封装好的contains函数进行判断,

如果要位置的话可以使用indexOf进行定位;

源码如下:

#include <QApplication>
#include <QString>
#include <QDebug>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QString testStr = "111223344 #$%% ****#*** f#ky";

    if(testStr.contains("1111")){
        qDebug()<< "1111" << "has been find!";
    }
    else{
        qDebug()<< "1111" << "was not find!";
    }

    qDebug()<< "indexOf position:" << testStr.indexOf("#");
    qDebug()<< "indexOf position:" << testStr.indexOf("#", 11);
    qDebug()<< "indexOf position:" << testStr.indexOf("#", 50);


    return a.exec();
}

程序运行截图如下:

总结

QTL中的QString操作起来还是爽一点,跟java差不多,爽得一逼;

猜你喜欢

转载自blog.csdn.net/qq78442761/article/details/84839711