我有一个像这样的html字符串:
<a class="favourite" href="LixWQfueLU"><a href="LixWQfueLU"><font color="#009a49">Rohit Lalwani</font></a></a>
我想制作html字符串:
<a class="favourite" href="LixWQfueLU"><font color="#009a49">Rohit Lalwani</font></a>
如何解决上述问题?我应该使用 JSOUP 还是正则表达式?什么是解决方案?
答案 0 :(得分:2)
使用JSoup的代码将以更通用的方式完成这一操作:
String html ="<a class=\"favourite\" href=\"LixWQfueLU\"><a href=\"LixWQfueLU\"><font color=\"#009a49\">Rohit Lalwani</font></a></a>";
Document doc = Jsoup.parse(html);
Element afav = doc.select(".favourite").first();
Element select = doc.select("font").first();
afav.remove();
afav.appendChild(select);
System.out.println(afav);
输出:
<a class="favourite" href="LixWQfueLU"><font color="#009a49">Rohit Lalwani</font></a>
答案 1 :(得分:1)
尝试使用子字符串获取所需的字符串:
String beforeString = "<p dir=\"ltr\"> <a class=\"favourite\" href=\"LixWQfueLU\"><a href=\"LixWQfueLU\"><font color=\"#009a49\">Rohit Lalwani</font></a></a></p>";
String afterString = beforeString.substring(0,beforeString.indexOf("<a href")+1)+beforeString.substring(beforeString.indexOf("<font"),beforeString.indexOf("</a>"))+beforeString.substring(beforeString.indexOf("</a>")+4,beforeString.length());
afterString的值:
<p dir="ltr"> <a class="favourite" href="LixWQfueLU"><<font color="#009a49">Rohit Lalwani</font></a></p>