QString remove from the end untill a specific character
Solved
General and Desktop
-
Hi, I'm trying to find a simple way to do this. I already made something that works, but it's a mess and I think there's a simpler way to do this.
Here's what I did:
QString test = "Android/media/com.whatsapp"; std::reverse(test.begin(), test.end()); QString ola = test.mid(0, test.indexOf("/")); std::reverse(ola.begin(), ola.end()); int ren = ola.count(); test.remove(0, ren); std::reverse(test.begin(), test.end()); qDebug() << test; //prints "Android/media/"
Is there an easier way to do this?
-
test = test.left(test.lastIndexOf('/') + 1);
or
test.truncate(test.lastIndexOf('/') + 1);
or
test = test.section('/', 0, -2, QString::SectionIncludeTrailingSep);
-
-
test = test.left(test.lastIndexOf('/') + 1);
or
test.truncate(test.lastIndexOf('/') + 1);
or
test = test.section('/', 0, -2, QString::SectionIncludeTrailingSep);
-
test = test.left(test.lastIndexOf('/') + 1);
or
test.truncate(test.lastIndexOf('/') + 1);
or
test = test.section('/', 0, -2, QString::SectionIncludeTrailingSep);
Hi @Chris-Kawa, your solution is probably the shortest and simplest.
I'll use one of these 3 commands. ThanksThank you for replies from others.