删除两个字符之间的子字符串(java)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10462209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Removing a substring between two characters (java)
提问by Rickard
I have a java string such as this:
我有一个这样的java字符串:
String string = "I <strong>really</strong> want to get rid of the strong-tags!";
And I want to remove the tags. I have some other strings where the tags are way longer, so I'd like to find a way to remove everything between "<>" characters, including those characters.
我想删除标签。我还有一些其他字符串的标签更长,所以我想找到一种方法来删除“<>”字符之间的所有内容,包括这些字符。
One way would be to use the built-in string method that compares the string to a regEx, but I have no idea how to write those.
一种方法是使用内置字符串方法将字符串与正则表达式进行比较,但我不知道如何编写这些方法。
回答by Bohemian
Caution is advised when using regex to parse HTML (due its allowable complexity), however for "simple" HTML, and simple text (text without literal <
or >
in it) this will work:
建议在使用正则表达式解析 HTML 时要小心(由于其允许的复杂性),但是对于“简单”HTML 和简单文本(没有文字<
或其中的文本>
),这将起作用:
String stripped = html.replaceAll("<.*?>", "");
回答by Gibolt
To avoid Regex:
为了避免正则表达式:
String toRemove = StringUtils.substringBetween(string, "<", ">");
String result = StringUtils.remove(string, "<" + toRemove + ">");
For multiple instances:
对于多个实例:
String[] allToRemove = StringUtils.substringsBetween(string, "<", ">");
String result = string;
for (String toRemove : allToRemove) {
result = StringUtils.remove(result, "<" + toRemove + ">");
}
Apache StringUtilsfunctions are null-, empty-, and no match- safe
Apache StringUtils函数是空、空和无匹配安全的