如何查找和替换字符串中的 <br> 标签?
How to find and replace <br> tag in a string?
我需要查找一个字符串是否包含 <br>
标签,我希望用 space 替换该标签我已经尝试过但无济于事:
if (cell.contains("<br\/>")) {
cell = cell.replace("<br\/>"," ");
}
您似乎在尝试转义输入中的特殊字符 String
,但这不是必需的; String
:
上的 replace()
方法
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
好简单
cell = cell.replace("<br>"," ");
将在您的字符串中用 " "
替换每个 "<br>"
实例。
这与 replaceFirst()
and replaceAll()
形成对比,后者都采用正则表达式和替换。
此外,不需要 if 语句。如果未找到目标序列,replace()
将简单地 return 原始字符串,因此 if 是多余的。
找到答案了。
不知道为什么
cell = cell.replace("<br>"," ");
对我不起作用。
另一方面,这做到了
cell = cell.replaceAll("[\t\n\r]"," ");
我需要查找一个字符串是否包含 <br>
标签,我希望用 space 替换该标签我已经尝试过但无济于事:
if (cell.contains("<br\/>")) {
cell = cell.replace("<br\/>"," ");
}
您似乎在尝试转义输入中的特殊字符 String
,但这不是必需的; String
:
replace()
方法
Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence.
好简单
cell = cell.replace("<br>"," ");
将在您的字符串中用 " "
替换每个 "<br>"
实例。
这与 replaceFirst()
and replaceAll()
形成对比,后者都采用正则表达式和替换。
此外,不需要 if 语句。如果未找到目标序列,replace()
将简单地 return 原始字符串,因此 if 是多余的。
找到答案了。
不知道为什么
cell = cell.replace("<br>"," ");
对我不起作用。
另一方面,这做到了
cell = cell.replaceAll("[\t\n\r]"," ");