正则表达式替换为“”不起作用 - Java

时间:2014-03-28 19:54:45

标签: java regex

我试图用""替换字符串中的所有 但下面似乎没有用。

str.replace("&nbsp","");

我的字符串:

<img alt="" src="abc430.jpg" width="650" height="430" /> u seen&nbsp; <a  
href="http://www.funnyordie.com/between_two_ferns" target="_blank"></a>&nbsp;hey hey hey

trying to get this output: 
<img alt="" src="abc430.jpg" width="650" height="430" /> u seen <a  
href="http://www.funnyordie.com/between_two_ferns" target="_blank"></a>hey hey hey

现在替换代码确实替换了&amp; NBSP;用&#34;&#34;但我的页面输出仍然是

u seen 
link here
hey hey

不在一行

1 个答案:

答案 0 :(得分:4)

有效。您没有将替换的字符串分配到str

str = str.replace("&nbsp;","");

尝试从最终运行此代码:

String str = "alex &nbsp; alex";
str = str.replace("&nbsp;","");
System.out.println(str);

输出:

alex  alex