Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QString Splitting

Tags:

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?

like image 957
Wazery Avatar asked Aug 01 '12 02:08

Wazery


People also ask

What is a QString?

QString stores unicode strings. By definition, since QString stores unicode, a QString knows what characters it's contents represent. This is in contrast to a C-style string (char*) that has no knowledge of encoding by itself.

How do you declare a QString?

One way to initialize a QString is simply to pass a const char * to its constructor. For example, the following code creates a QString of size 5 containing the data "Hello": QString str = "Hello"; QString converts the const char * data into Unicode using the fromAscii() function.

How do you append in QString?

Try this: double d1 = 0.5,d2 = 30.0 QString str = "abc"; str. append(QString("%1"). arg(d1)); str.


1 Answers

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.

like image 159
Chris Avatar answered Sep 30 '22 19:09

Chris