PHP has strip_tags function which strips HTML and PHP tags from a string.
Does Android have a way to escape html?
PHP has strip_tags function which strips HTML and PHP tags from a string.
Does Android have a way to escape html?
Html.fromHtml can be extremely slow for large html strings.
Here's how you can do it, easily and fast with jsoup:
Add this line to your gradle file:
implementation 'org.jsoup:jsoup:1.11.3'
Check what is the latest jsoup version here: https://jsoup.org/download
Add this line to your code:
String text = Jsoup.parse(htmlStr).text();
Check this link here to learn how to preserve line breaks:
How do I preserve line breaks when using jsoup to convert html to plain text?
As it has not been mentioned yet, the way to do this in a backwards compatible manner would be to use the HtmlCompat utility class, and simply call (with 0 if you require no specific flags to be used)
HtmlCompat.from(inputString, 0).toString()
Under the hood it already does all the required api checks for you
if (Build.VERSION.SDK_INT >= 24) {
return Html.fromHtml(source, flags);
}
return Html.fromHtml(source);
So for for the input
<a href="https://www.stackoverflow.com">Click me!</a>
you will receive only the string 'Click me!' as output.