QString - Get the characters from Index X to Index Y
-
Hi, I'm trying to figure out how to get the String's characters from index X to index Y.
For example:Qstring filename = "blabla_12345.txt" int dotIndex = filename.lastIndexOf(".");
now I want to get the characters from index X till the dotIndex.
For example, I want to have a QstringfileNumber
, which contains the characters from 6 to dotIndex ("_12345").
Or in other case, I want to have a QString that contains characters from 2 to dotIndex ("abla_12345")P.S (I have already figured out a way to do this by performing multiple Qstring operations, but I'm wondering if there is a way to get it done in a single command.)
-
-
Yes I've seen the documentation but which of these functions allow me to do exactly what I want?
As I've said, I have already figured out a way to do this by performing multiple Qstring operations, but I'm wondering if there is a way to get it done in a single command. -
@jellyv said in QString - Get the characters from Index X to Index Y:
As I've said, I have already figured out a way to do this by performing multiple Qstring operations, but I'm wondering if there is a way to get it done in a single command.
So can you show us what you tried?
-
@jellyv said in QString - Get the characters from Index X to Index Y:
Yes I've seen the documentation but which of these functions allow me to do exactly what I want?
QString::mid()?
-
@Christian-Ehrlicher
For example, I want to have a Qstring fileNumber , which contains the characters from 7 to dotIndex ("_12345").
In this case, I had to find the dotIndex, get the string from beginning to dotindex, and then get the string from 6th character till the end.int dotIndex = filename.lastIndexOf("."); QString withoutDot = filename.mid(0, dotIndex); QString fileNumber = withoutDot.mid(7, withoutDot.length());
What I WANT to do is to get the string from X-th (7 in this case) character till the dotIndex in one command.
In my program, X changes, but dotIndex always remains the same.
For example, when X is 4, I want to get the string from 4th character till the dotIndex -
@raven-worx already gave you a hint how to do it - QString::mid() is the way to go.
-
@Christian-Ehrlicher
I don't think I've made myself clear here....Take this example:
QString x = "Nine pineapples"; QString y = x.mid(5, 4); // y == "pine"
QString::mid()
lets you get 4 characters after the 5fth character (pine)
I'm looking for a void where you can say: give me characters 5 to 10 (pineap) (5th, 6th, 7th, 8th, 9th and 10th characters)
In other words, I'm looking forQString::mid()
's alternative which lets you specify the ending position -
@jellyv said in QString - Get the characters from Index X to Index Y:
alternative which lets you specify the ending position
mid(start, len) = mid (start, end - len + 1) - simply math, isn't it?
mid(5, 6) = mid(5, 10 - 6 + 1)