really want to get rid...">

Removing a substring between two characters (java)

I have a java string like this:

String string = "I <strong>really</strong> want to get rid of the strong-tags!";

And I want to remove tags. I have a few lines where the tags are longer, so I would like to find a way to remove all the "<>" characters, including these characters.

One way is to use the built-in string method, which compares the string against regEx, but I have no idea how to write them.

+6
source share
2 answers

When using a regular expression, it is recommended to parse HTML (due to its admissible complexity), however for “plain” HTML and plain text (text without <or >in a literal ) this will work:

String stripped = html.replaceAll("<.*?>", "");
+17
source

To avoid regex:

String toRemove = StringUtils.substringBetween(string, "<", ">");
String result = StringUtils.remove(string, "<" + toRemove + ">"); 

:

String[] allToRemove = StringUtils.substringsBetween(string, "<", ">");
String result = string;
for (String toRemove : allToRemove) {
  result = StringUtils.remove(result, "<" + toRemove + ">"); 
}

Apache StringUtils null-, empty- match-

0

All Articles