如何从'<'中删除整个子字符串到'>'在Java中 [英] How to remove entire substring from '<' to '>' in Java

查看:86
本文介绍了如何从'<'中删除整个子字符串到'>'在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?"

我希望输出字符串只包含PS你好今天你好吗? ?。在Java中有一种简单的方法吗?谢谢

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("<[^>]*>", "");

这意味着找到以< ,然后是> 的任意数量的字符,然后是字符> 。然后用空字符串替换所有这些子串,

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, "".

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

这篇关于如何从'&lt;'中删除整个子字符串到'&gt;'在Java中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆