Reputation: 1124
how can i get some text between each two tags(<tagname></tagname>
) and after changed the text of these two tags i need the complete string along with changes.think,this is the java string content.
before change
<tagname>text to be changed 1</tagname>
amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud <tagname>text to be changed 2</tagname>
laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint <tagname>text to be changed 3</tagname>
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
after changed, should be like this
<tagname>text changed 1</tagname>
amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud <tagname>text changed 2</tagname>
laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint <tagname>text changed 3</tagname>
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
actually i need a method like this,
String getChangedString(String toBeChanged){
//process
return changedString;
}
how can i get this.
thanks.
Upvotes: 1
Views: 6311
Reputation: 3154
There is a flawless string parsing based utility available in org.apache.commons.lang.StringUtils, below code worked perfectly for me.
String title = StringUtils.substringBetween(testHtml, "<title>", "</title>");
System.out.println("title:" + title); // good
Upvotes: 1
Reputation: 81
I was looking for the same, maybe this question can help you, take a look: Java regex to extract text between tags
It uses a regular expression to get the text content between tags. In my case, I was interested on getting the text from the first tags found on my xml, not from each tags
Upvotes: 1
Reputation: 15212
Assuming that the tags are within an XML file, you should use one of the existing XML parsers such as DOM or SAX instead of reinventing the wheel.
Upvotes: 1
Reputation:
String getChangedString(String toBeChanged){
String changedString = toBeChanged;
while (changedString.indexOf("<tagname>") >= 0)
{
changedString = changedString.substring(0, changedString.indexOf("<tagname>")) +
"whatever replacement string" +
changedString.substring(changedString.indexOf("</tagname>") + "</tagname>".length());
}
return changedString;
}
Upvotes: 2