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
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.
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.
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.
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With