Unsolved Pull apart String
-
Hello. How Can I pull apart a section of QString?
For example,
QString chain; chain.append("The real"):
I want to have two QString, one with "The" and the other with "real". What I have tried it's to find first the space between words
QString chain; QString aux; QString aux2; chain.append("The real"): //let's find space's position while(chain[position]!=" ") { position++; } //Now, make the two QString for(int i=0; i<position; i++) { aux.push_back(chain[i]); } for(int i=position+1;i<chain.length(); i++) { aux2.push_bach(chain[i]); //Now aux must be "The " and aux2 must be "real"
But this code do not work. Why.
-
Well, you have puxh_bach and push_back in the same code example. I'm not internationally versed so if that's german or something and the compiler is cool with building that... dunno.
Sure there is push back and all but I am not sure how much fun you'll have trying to access QStrings like Char arrays? Your codes' intention and purpose is not obvious. Not sure the rule for having the first one include the space and the second not either.
I think it would be helpful for you to know of this:
If you just want the data between spaces (or another delimiter) you could use like something like:
QString sData = getData();
QStringList stringList = sData.split(" ", QString::SplitBehavior::KeepEmptyParts);Hopefully helpful...
-
Thank. QString::split() it great!
-
Like @6thC already said,
split()
is your friend. For more info on string handling, I recommend readingQString
's documentation page, it's great: QString Class :-) -
Please consider splitRef() instead of
split()
. It does not make copies. Also if you only want one part of the string take a look at section().