[英]How to remove > tag from string in java
我有以下字符串。 "Christmas is a very expensive> time of year for most> people so the <br/>Christmas> bazaar is an <b>opportunity</b> for parents to indulge their offspring and buy lots> of small items> as either presents or <b>stocking fillers</b> .|Trust me, it's not easy scooping up gift votives and <b>stocking stuffers</b>"
。
现在我只想从以单词结尾的字符串中只删除">"
而不是 html 标签,例如"<br/> or <b"
如果我使用String.replace("\\\\>","")
那么它会从字符串中删除所有>
标记。
怎样才能做到这一点?
我已经完成了一个灵活高效的HTML解析器,可在此处下载:
这是你的问题解决了:
import Torello.HTML.*;
import Torello.Java.*;
import java.util.*;
import java.io.*;
public class ReplaceGreaterThan
{
public static void main(String[] argv) throws IOException
{
String YOUR_STRING_VAR = "Christmas is a very expensive> time of year for most> people so the <br />Christmas> bazaar is an <b>opportunity</b> for parents to indulge their offspring and buy lots> of small items> as either presents or <b>stocking fillers</b> .|Trust me, it's not easy scooping up gift votives and <b>stocking stuffers</b>";
Vector<HTMLNode> page = HTMLPage.getPageTokens(YOUR_STRING_VAR, false);
HTMLNode n;
for (int i=0; i < page.size(); i++)
if ((n = page.elementAt(i)) instanceof TextNode)
if (n.str.contains("<") || n.str.contains(">"))
page.setElementAt(new TextNode(n.str.replaceAll("(<|>)", "")), i);
YOUR_STRING_VAR = HTMLNodeFunction.pageToString(page);
System.out.println(YOUR_STRING_VAR);
}
}
这是输出:
圣诞节是一年中大多数人非常昂贵的时间,所以在圣诞集市是家长放纵自己的后代和购买大量的小项目,如任何礼物或放养填料 机会 |。相信我,这是不容易挖出礼物votives和放养灌肠机
检查以下代码是否符合您的需要:
String[] split = "This is test string> <br></br>".split(">");
StringBuilder sb = new StringBuilder();
for (String it : split) {
if(it.contains("<")) {
it += ">";
}
sb.append(it);
}
String result = sb.toString();
您可以使用String.replace(“string>”,“string”)来实现结果。 如果这不能解决您的问题,请提供更多详细信息。
如果您只需要删除第一个出现的">"
,请使用replacefirst(正则表达式,“new-value”);
System.out.println("This istest string> <br></br>".replaceFirst(">",""));
输出:
This istest string <br></br>
编辑:根据你的评论,“但我需要替换所有以字符串中的单词结尾的”>“。
使用“ positive lookbehind
”
(?<=String)>
(正向lookbehind)与string>
中的>(和唯一>)匹配,但与somethingelse>不匹配。
System.out.println("This istest string> <br></br>".replaceFirst("(?<=string)>",""));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.