我需要查找字符串是否包含 <br>
标签,我希望用空格替换该标签,但我已经尝试过此操作,但无济于事:
if (cell.contains("<br\\/>")) {
cell = cell.replace("<br\\/>"," ");
}
最佳答案
看起来您正在尝试转义输入中的特殊字符 String
,但这不是必需的; replace()
方法String
:
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
就这么简单
cell = cell.replace("<br>"," ");
将替换 "<br>"
的每个实例与 " "
在你的字符串中。
这与 replaceFirst()
形成对比和 replaceAll()
它们都采用正则表达式和替换。
此外,不需要 if 语句。如果未找到目标序列,replace()
只会返回原始字符串,因此 if 是多余的。
关于java - 如何查找并替换字符串中的 <br> 标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37215431/