How to remove entire substring from '<'

2019-03-15 23:42发布

问题:

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

回答1:

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()



回答2:

If you would prefer to avoid using regular expressions you can use substring:

String origText = "P.S.<!--"+
    "BODY"+
       "{"+
       "color:white;"+
       "background-color: transparent;"+
       "font-family:sans-serif;"+
       "}"+
    "--> Hello how are you today?";
String revised = origText.substring(0, origText.indexOf('<')) +
    origText.substring(origText.lastIndexOf('>')+1, origText.length());

`Java String Class Reference