首页 > 解决方案 > 为什么我不能从变量中删除 div 标签?

问题描述

我正在尝试使用Jsoup库从变量中删除 div 标签,但它不适用于我。

代码

String test = "<div>hey</div>";
Document doc = Jsoup.parse(test);
doc.select("div").remove();
Log.w("log",test);

结果

2019-08-30 19:25:24.206 314-3434/com.test.app W/log: <div>hey</div>

标签: android

解决方案


为什么不使用String.replace()方法?例如:

String test = "<div>Hey<div>";
String newTest = test.replace("<div>", "");

或者尝试使用 JSoup 删除,

 doc.getElementsByTag("div").remove();

推荐阅读