Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any method to identify whether a string contains HTML tags in java

Tags:

java

html

string

Is there any predefined method stating whether a string contains HTML tags or characters in it?

like image 486
Neha S Avatar asked Aug 18 '15 06:08

Neha S


People also ask

How do I check if a string contains HTML?

test. bind(/(<([^>]+)>)/i); It will basically return true for strings containing a < followed by ANYTHING followed by > .

How do you check if a string contains a certain character in Java?

Use String contains() Method to Check if a String Contains Character. Java String's contains() method checks for a particular sequence of characters present within a string. This method returns true if the specified character sequence is present within the string, otherwise, it returns false .

What is an HTML string?

Unlike most HTML parsers which generate tree structures, HTMLString generates a string of characters each with its own set of tags. This flat structure makes it easy to manipulate ranges (for example - text selected by a user) as each character is independent and doesn't rely on a hierarchical tag structure.


1 Answers

Either Use regular expression to search or identify the HTML tags in String.

boolean containsHTMLTag = stringHtml.matches(".*\\<[^>]+>.*");

Or as Tim suggested use Jsoup like below:-

String textOfHtmlString = Jsoup.parse(htmlString).text();
boolean containedHTMLTag = !textOfHtmlString.equals(htmlString);
like image 69
Amit Bhati Avatar answered Sep 27 '22 20:09

Amit Bhati