QT获取字符所有子字符串位置,删除所有子字符串

⌚Time: 2023-09-22 13:54:53

👨‍💻Author: Jack Ge

字符串定义


QString str = "qwe ert dfder fgg";

QString child = "er";

获取所有子串位置


    int index = str.indexOf(child);

    while(-1 != index){

        qDebug()<<"index"<<index;

        index = str.indexOf(child,index+1);

    }

删除所有子串


    index = str.indexOf(child);

    while(-1 != index){

        str.remove(index,child.length());

        index = str.indexOf(child);

    }

    qDebug()<<str;

输出


index 4

index 11

"qwe t dfd fgg"