我有这样的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还是Regex?解决办法是什么?
最佳答案
尝试使用substring获取所需的字符串:
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>