如何在Java中将整个子字符串从“”(How to remove entire substring from '' in Java)

系统教程 行业动态 更新时间:2024-06-14 16:57:39
如何在Java中将整个子字符串从“<”移动到“>”(How to remove entire substring from '<' to '>' in Java)

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

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

更多推荐

本文发布于:2023-04-13 12:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/6bc62988596978abc135cb78eacbc2d0.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   中将   如何在   Java   substring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!