I have the following string. "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>"
.
Now I want to remove only ">"
from the strings that ended with with words only not the html tags such as "<br/> or <b"
If I use String.replace("\\\\>","")
then it remove all the >
tag from the string.
How can achieve that ?
I have completed a nimble and efficient HTML parser available for download here:
Here is your question solved:
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);
}
}
Here is the output:
Christmas is a very expensive time of year for most people so the Christmas bazaar is an for parents to indulge their offspring and buy lots of small items as either presents or .|Trust me, it's not easy scooping up gift votives and |。相信我,这是不容易挖出礼物votives和
Check if the following code suit your need:
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();
You can use String.replace("string>", "string") to achieve the result. If this doesn't solve your problem please give more details.
If you only need to remove the first occurrence of ">"
, use replacefirst(regex,"new-value");
System.out.println("This istest string> <br></br>".replaceFirst(">",""));
Output:
This istest string <br></br>
EDIT: Per your comment,"but i need to replace all of the ">" that ended with word from the string".
Use " positive lookbehind
"
(?<=String)>
(positive lookbehind) matches the > (and only the >) in string>
, but does not match somethingelse>.
System.out.println("This istest string> <br></br>".replaceFirst("(?<=string)>",""));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.