I want to know how to get the string out of a group of strings
String j = "<a ..........开发者_JAVA百科..> hai</a>";
I want to get the string for the tag
<a ...> ... </a>
I'd use an XML parser for that and extract the attributes in question.
I agree with Bozho. If you need to do this on a regular basis, an XML or HTML Parser would be much less error prone.
For a quick and dirty approach, you can use the Regex
(href|src)="[^"]*"
Make sure to escape all these quotes when trying that.
I hate regex, I would have done it this way..
str = str.substring(str.indexOf("href="));
str = str.substring(0, str.indexOf("\"", 5); //'href=' is 5 chars
//str = str.substring(0, str.indexOf(" "); //this is more readable I think
Do the same for src=
Note, you will get href=
as part of the string as well, how to prevent that is left as an exercise. lol. (HINT: change 0
in the second line to ...)
精彩评论