How to Convert HTML Text to Plain Text in Android

1. Using Html.fromHtml()

String htmlText = "<p>This is some HTML text.</p>";
String plainText = Html.fromHtml(htmlText).toString();
This is some HTML text.

2. Using Jsoup

String htmlText = "<p>This is some HTML text.</p>";
Document doc = Jsoup.parse(htmlText);
String plainText = doc.text();
This is some HTML text.

3. Using Regex

String htmlText = "<p>This is some HTML text.</p>";
String plainText = htmlText.replaceAll("<[^>]+>", "");
This is some HTML text.

Comparison

Method Advantages Disadvantages
Html.fromHtml() Simple and built-in Can’t handle all HTML tags
Jsoup Handles all HTML tags External library required
Regex Customizable Can be complex and inefficient

Choosing the Right Method

  • For simple HTML, Html.fromHtml() is the easiest option.
  • For complex HTML, Jsoup is more reliable.
  • Regex can be used for more control over the conversion process.

Leave a Reply

Your email address will not be published. Required fields are marked *