Remove HTML tags from a String

To remove HTML tags from a string, you can use a regular expression to match and replace the tags with an empty string.

Here's an example of how you can do this in Java:

import java.util.regex.Pattern;

public static String stripHtmlTags(String input) {
  if (input == null || input.isEmpty()) {
    return input;
  }
  // Compile the regular expression to match HTML tags
  Pattern pattern = Pattern.compile("<[^>]+>");
  // Replace all HTML tags with an empty string
  return pattern.matcher(input).replaceAll("");
}

This function uses a regular expression to match any sequence of characters that starts with a < character and ends with a > character, and replaces it with an empty string. This will remove all HTML tags from the input string.

Here's an example of how you can use the stripHtmlTags() function:

String input = "<p>This is a <b>test</b> string.</p>";
String output = stripHtmlTags(input);
System.out.println(output); // This is a test string.

Note that this function only removes the tags from the input string, and does not parse the HTML to extract the content of the tags. If you need to extract the content of the tags, you will need to use an HTML parser or a library that can parse HTML.