Java String Replace '&' with & but not & to &

旧时模样 提交于 2019-12-04 23:55:35

You can use a regular expression with a negative lookahead.

The regex string would be &(?!amp;).

Using replaceAll, you would get:

A&B
A & B
A& B
A &B
A&B
A & B
A& B
A &B

So the code for a single string str would be:

str.replaceAll("&(?!amp;)", "&");

You can try this, it should work:

data = data.replaceAll("&","&").replaceAll("&","&");

That way you first replace all & with & so all you'll have is &, and then, you replace all of them with &.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!