Home  >  Article  >  Java  >  How to remove HTML tags from given string in Java?

How to remove HTML tags from given string in Java?

PHPz
PHPzforward
2023-08-29 18:05:061185browse

How to remove HTML tags from given string in Java?

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.

Syntax

public String replaceAll(String regex, String replacement)

Example

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);
   }
}

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete