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=""?

有帮助吗?

解决方案

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

其他提示

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);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top