Reputation: 9121
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=""
?
Upvotes: 0
Views: 568
Reputation: 32645
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);
Upvotes: 0
Reputation: 32393
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
Upvotes: 1