Well, duplication is only necessary because you don't have the substring in a variable.
const QString substring
("something");
str.left(str.indexOf(substring) + substring.length());
const QString substring("something");
str.left(str.indexOf(substring) + substring.length());
To copy to clipboard, switch view to plain text mode
Alternatively, a regular expression match could be used to extract the result. Something like
QRegularExpression pattern("^(.*)something"); // any characters from the beginning of the string until "something"
QRegularExpressionMatch match = pattern.match(str);
if (match.hasMatch()) {
// result in match.captured(1);
}
QRegularExpression pattern("^(.*)something"); // any characters from the beginning of the string until "something"
QRegularExpressionMatch match = pattern.match(str);
if (match.hasMatch()) {
// result in match.captured(1);
}
To copy to clipboard, switch view to plain text mode
Probably only worth though it if the you look for the same pattern in a lot of string.
Cheers,
_
Bookmarks