This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes JSP and the fly likes StringUtils.substringBetween issue, please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "StringUtils.substringBetween issue, please help" Watch "StringUtils.substringBetween issue, please help" New topic

StringUtils.substringBetween issue, please help

Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
I have the following code which parses an XML feed, strips out the <ul> tags for each row of data. I am then left with in some cases multiple <li> tags within a single string each of which contain an <a> tag. The code then proceeds to get the value between each <a> tag located within each <li>. However the StringUtils.substringBetween function is only dealing with the first <a> tag and disregarding any other <a> tags found in the same string. The code to display the number of occurences is correct, but I am at a loss of why the stringBetween function is only dealing with one <a> tag per row of data instead of the possibility of multiple <a> tags within a sign string:

String suitableStrip = Suitable.item(i).getFirstChild().getNodeValue().trim();
suitableStrip = utils.replace(suitableStrip, "<ul>", "");
suitableStrip = utils.replace(suitableStrip, "</ul>", "");
String modelStrip = StringUtils.substringBetween(suitableStrip, "\">", "<");

int occurrences = 0;
int index = 0;
while (index < suitableStrip.length() && (index = suitableStrip.indexOf("<li>", index)) >= 0) {

Any help would be much appreciated :-)
Gary Sheldon
Ranch Hand

Joined: Nov 21, 2011
Posts: 44
Oops, didnt realise there was a substringsBetween option, simply modified modelStrip as follows:

String modelStrip[] = StringUtils.substringsBetween(suitableStrip, "\">", "<");

Hope this proves useful to someone else :)
I agree. Here's the link:
subject: StringUtils.substringBetween issue, please help
It's not a secret anymore!