Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing a substring between two characters (java)

I have a java string such as this:

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

And I want to remove the tags. I have some other strings where the tags are way longer, so I'd like to find a way to remove everything between "<>" characters, including those characters.

One way would be to use the built-in string method that compares the string to a regEx, but I have no idea how to write those.

like image 419
Rickard Avatar asked May 05 '12 13:05

Rickard


People also ask

How do I extract a string between two characters?

To extract part string between two different characters, you can do as this: Select a cell which you will place the result, type this formula =MID(LEFT(A1,FIND(">",A1)-1),FIND("<",A1)+1,LEN(A1)), and press Enter key. Note: A1 is the text cell, > and < are the two characters you want to extract string between.

How do I remove a substring from a string in Java?

In order to remove a substring from a Java StringBuilder Object, we use the delete() method. The delete() method removes characters in a range from the sequence. The delete() method has two parameters, start, and end. Characters are removed from start to end-1 index.

How do you replace text between two characters in Java?

The replace() method takes two arguments – target and replacement text: String master = "Hello World Baeldung!"; String target = "Baeldung"; String replacement = "Java"; String processed = master. replace(target, replacement); assertTrue(processed. contains(replacement)); assertFalse(processed.


2 Answers

Caution is advised when using regex to parse HTML (due its allowable complexity), however for "simple" HTML, and simple text (text without literal < or > in it) this will work:

String stripped = html.replaceAll("<.*?>", "");
like image 111
Bohemian Avatar answered Sep 28 '22 04:09

Bohemian


To avoid Regex:

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

For multiple instances:

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

Apache StringUtils functions are null-, empty-, and no match- safe

like image 28
Gibolt Avatar answered Sep 28 '22 05:09

Gibolt