QString Splitting

Wazery picture Wazery · Aug 1, 2012 · Viewed 61.7k times · Source

I have these url strings

file:///home/we/Pictures/neededWord/3193_n.jpg

file:///home/smes/Pictures/neededWord/jds_22.png

file:///home/seede/kkske/Pictures/neededWord/3193_n.jpg

I want to extract the "neededWord" from each of them. As it appears from them, the name of the image is always after the "neededWord" and the changing part in the string is before the "neededWord". The way I thought of is to split the string using the "/" seperator from right and take the second element in the resulted QstringList. So how to split from right, or is there a better way to do that?

Answer

Chris picture Chris · Aug 1, 2012

Well you would just take the second to last element:

QStringList pieces = url.split( "/" );
QString neededWord = pieces.value( pieces.length() - 2 );

Alternatively, you could use a regular expression.