Qt Substring Extraction Guide: mid() Method

In Qt, the method for substring extraction is using the “mid” function, which takes two parameters: the starting position and the length of the substring to extract. An example is shown below:

QString str = "Hello, World!";
QString subStr = str.mid(7, 5); // 从位置7开始截取长度为5的子字符串
qDebug() << subStr; // 输出 "World"

In addition to the mid function, you can also use the left and right functions to extract the left and right parts of a string. Examples are as follows:

QString str = "Hello, World!";
QString leftStr = str.left(5); // 截取左边5个字符
QString rightStr = str.right(6); // 截取右边6个字符
qDebug() << leftStr; // 输出 "Hello"
qDebug() << rightStr; // 输出 "World!"
bannerAds