0

I want to remove HTML of string in Java.

<hr><b><strong>Task Details</strong></b><hr><b>Date Created: </b> 01/06/2014 07:55pm<br><b>Date Modified: </b> 01/06/2014 07:55pm<br><b>Assigned to: </b> Administrator<br><b>Created By: </b> Administrator<br><b>Status: </b> Not Started<br><b>Description: </b> Test Description<br>.

Above is my string and I want to remove HTML tags.

4
  • Are these HTML tags? The input does not seem to have HTML tags. Commented Jan 9, 2014 at 6:59
  • the question is not clear enough. What string are you talking about and what HTML tag ? Can you please elaborate the question and mention what is it that you exactly want to achieve? Commented Jan 9, 2014 at 6:59
  • 'Java' is to 'JavaScript' as 'Car' is to 'Carpet'. So which language are you using? Commented Jan 9, 2014 at 7:15
  • I'm just going to leave both [java] and [javascript] in the question until you figure out which language you're using... please retag it accordingly. Commented Mar 10, 2014 at 4:09

2 Answers 2

4

Maybe this will work:

String noHTMLString = htmlString.replaceAll("\\<.*?>","");

It uses regular expressions to remove all HTML tags in a string.

More specifically, it removes all XML like tags from a string. So <1234> will be removed even though it is not a valid HTML tag. But its good for most intents and purpouses.

Hope this helps.

This is actually dead simple with Jsoup.

public static String html2text(String html) {
    return Jsoup.parse(html).text();
}
Sign up to request clarification or add additional context in comments.

1 Comment

-1 The first part of your answer up to the "Hope this helps" is copied verbatim and unattributed from this earlier answer. Unless you're the same poster, that's a big netiquette no-no.
1

You can use Jsoup library for it.

String str="<h3>My Text</h3>";
System.out.println(Jsoup.parse(str).text());

The above code strip all htms tags and give text left as output

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.