Bernheart
Bernheart

Reputation: 637

Manipulate regular String in Java

I have a String text that has a regular form and want to take two parts of this String. the String has the format

"<html><div style=\"text-align:center;\"><b>****</b><br><i>Aula: </i><b>****</b></div></html>"

Where the ****indicates the parts of string that I want to take. How can I do? I'm using JAVA, also the string is written in HTML.

We can see that the intresting parts of the String are both limited by <b> and <\b>

Upvotes: 0

Views: 93

Answers (2)

Pshemo
Pshemo

Reputation: 124285

If that is exactly form of your HTML String then you can use substring method using positions of <b> and </b> (if your HTML code can change you should use HTML parser)

String s = "<html><div style=\"text-align:center;\"><b>first</b><br><i>Aula: </i><b>second</b></div></html>";
int start = s.indexOf("<b>");
int end = s.indexOf("</b>");
String firstMatch = s.substring(start + "<b>".length(), end);

//now we can start looking for next `<b>` after position where we found `</b>`
start = s.indexOf("<b>", end);
//and look for </b> after position that we found latest <b>
end = s.indexOf("</b>", start);
String secondMatch = s.substring(start + "<b>".length(), end);

System.out.println(firstMatch);
System.out.println(secondMatch);

output:

first
second

Upvotes: 5

Daniel Kaplan
Daniel Kaplan

Reputation: 67554

You have a few options. The most obvious, but probably not the best, is to use a regex. Look at String.replaceAll for that.

A better option is to use an HTML parser. An example of that is JSoup.

Upvotes: 4

Related Questions