如何在Java中将整个子字符串从'<'移除到'>'

Sea*_*ean 9 java string substring

我有一个带输入字符串的程序.我想删除字符'<'和'>'中的任何内容.例如,如果字符串说

"P.S.<!--
BODY
   {
   color:white;
   background-color: transparent;
   font-family:sans-serif;
   }
--> Hello how are you today?"
Run Code Online (Sandbox Code Playgroud)

我希望输出字符串只包含"P.S. Hello how are you today?".在Java中有一种简单的方法吗?谢谢

Nay*_*uki 23

使用正则表达式:

newstr = str.replaceAll("<[^>]*>", "");
Run Code Online (Sandbox Code Playgroud)

这意味着找到每个<以字母开头的子字符串,然后是任意数量的字符,>然后是字符>.然后用空字符串替换所有这些子串"".

参考:java.lang.String.replaceAll()