Qt字节数组QByteArray的成员函数QByteArray__lastIndexOf和QByteArray__indexOf

⌚Time: 2022-06-14 22:38:03

👨‍💻Author: Jack Ge


int QByteArray::lastIndexOf(const QByteArray &ba, int from = -1) const

返回该字节数组中最后一次出现字节数组ba的索引位置,从索引位置向后(backward)搜索。如果from值是-1(默认值),搜索会从最后一个字节开始。若是找不到ba,则返回-1 。

意思就是在QByteArray字节数组中搜索ba这个字符串最后出现的位置,指定从位置0到搜索到最后位置from的区间。而from默认是-1,代表从位置0搜索到数组末尾ba出现的最后位置

官方例子:


  QByteArray x("crazy azimuths");

  QByteArray y("az");

  x.lastIndexOf(y);           // returns 6

  x.lastIndexOf(y, 6);        // returns 6

  x.lastIndexOf(y, 5);        // returns 2

  x.lastIndexOf(y, 1);        // returns -1

int QByteArray::indexOf(const QByteArray &ba, int from = 0) const

返回该字节数组中第一次出现字节数组ba的索引位置,从索引位置向前(forward)搜索。若是找不到ba,则返回-1 。指定从from位置到字符数组末尾作为搜索区间,而from默认是0,代表默认从位置0搜索到字符数组末尾。

官方例子:


QByteArray x("sticky question");

QByteArray y("sti");

x.indexOf(y);               // returns 0

x.indexOf(y, 1);            // returns 10

x.indexOf(y, 10);           // returns 10

x.indexOf(y, 11);           // returns -1

另外,这两个函数都是有重载的,因此可以接受多种类型的字符变量。


int lastIndexOf(const QByteArray & ba, int from = -1) const

int lastIndexOf(const QString & str, int from = -1) const

int lastIndexOf(const char * str, int from = -1) const

int lastIndexOf(char ch, int from = -1) const

int indexOf(const QByteArray & ba, int from = 0) const

int indexOf(const QString & str, int from = 0) const

int indexOf(const char * str, int from = 0) const

int indexOf(char ch, int from = 0) const