Question

I have a QString that contains:

<img class="openFile" data-id="../uploads/536b5621d91df1.76481105.png" src="../uploads/536b5621d91df1.76481105.png" />
iOS Simulator Screen shot 7 apr 2014 15.32.12.png

How can I extract whats inside src=""?

Was it helpful?

Solution

This answer should help you: https://stackoverflow.com/a/12432788/1221512

So your code should look like this:

QString data("<img class=\"openFile\" data-id=\"../uploads/536b5621d91df1.76481105.png\" src=\"../uploads/536b5621d91df1.76481105.png\" /> iOS Simulator Screen shot 7 apr 2014 15.32.12.png");
QString extractedData = data.section("src=\"",1).section("\"",0,0);

Also, may I suggest to use regular expression in combination with QString::filter()?

http://qt-project.org/doc/qt-4.8/qstringlist.html#filter

http://qt-project.org/doc/qt-5/QRegExp.html

OTHER TIPS

You can use QString::indexOf which returns the index position of the first occurrence of the string. After retrieving the indexes for the start and end, you can get the text by QString::mid :

int index1= str.indexOf ( "src=\"", 0);
index1+=5;

int index2 = str.indexOf("\"",index1);

QString src = str.mid(index1,index2-index1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top