我有一个输入字符串的程序。 我想删除字符'<'和'>'中的任何内容。 例如,如果字符串说
"P.S.<!-- BODY { color:white; background-color: transparent; font-family:sans-serif; } --> Hello how are you today?"我希望输出字符串只包含"PS Hello how are you today?" 。 有没有简单的方法来在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
最满意答案
使用正则表达式:
newstr = str.replaceAll("<[^>]*>", "");这意味着找到每个以<开始的子串,然后是任何数量的不是>的字符,然后是字符> 。 然后用空字符串""替换所有这些子字符串。
参考: java.lang.String.replaceAll()
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()
更多推荐
发布评论