String is the final class in Java, it is immutable, which means we cannot change it The object itself, but we can change the reference to the object. HTML tags can be removed from a given string using the replaceAll() method of the String class. We can use regular expressions to remove HTML tags from a given string. After removing the HTML tag from the string, it will return a string as normal text.
public String replaceAll(String regex, String replacement)
public class RemoveHTMLTagsTest { public static void main(String[] args) { String str = "<p><b>Welcome to Tutorials Point</b></p>"; System.out.println("Before removing HTML Tags: " + str); str = str.<strong>replaceAll("\<.*?\>", "")</strong>; System.out.println("After removing HTML Tags: " + str); } }
Before removing HTML Tags: <p><b>Welcome to Tutorials Point</b></p> After removing HTML Tags: Welcome to Tutorials Point
The above is the detailed content of How to remove HTML tags from given string in Java?. For more information, please follow other related articles on the PHP Chinese website!