[英]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.