Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove entire substring from '<' to '>' in Java

I have a program that takes an input string. I want to delete anything inside the characters '<' and '>'. For example if the string says

"P.S.<!--
BODY
   {
   color:white;
   background-color: transparent;
   font-family:sans-serif;
   }
--> Hello how are you today?"

I want the output string to only contain "P.S. Hello how are you today?". Is there a simple way to do this in Java? Thanks

like image 853
Sean Avatar asked Aug 17 '11 22:08

Sean


People also ask

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 remove a substring from a string?

To remove a substring from a string, call the replace() method, passing it the substring and an empty string as parameters, e.g. str. replace("example", "") . The replace() method will return a new string, where the first occurrence of the supplied substring is removed.

How do you remove certain values from a string in Java?

The idea is to use the deleteCharAt() method of StringBuilder class to remove first and the last character of a string. The deleteCharAt() method accepts a parameter as an index of the character you want to remove. Remove last character of a string using sb. deleteCharAt(str.


1 Answers

Use a regular expression:

newstr = str.replaceAll("<[^>]*>", "");

What this means is to find every substring beginning with <, then any number of characters that are not >, and then the character >. Then replace all these substrings with the empty string, "".

Reference: java.lang.String.replaceAll()

like image 113
Nayuki Avatar answered Oct 01 '22 03:10

Nayuki