有一个字符串变量,包含*。但是我需要替换其中所以的*字符。
我写的代码没有实现:
text = text.replaceAll("*","");
text = text.replaceAll("*",null);
请大家帮忙解决,谢谢。
有一个字符串变量,包含*。但是我需要替换其中所以的*字符。
我写的代码没有实现:
text = text.replaceAll("*","");
text = text.replaceAll("*",null);
请大家帮忙解决,谢谢。
用String#replace() 方法实现多简单,这个方法不会将regex当做参数。
text = text.replace("*","");
相反的,String#replaceAll() 将regex当做第一个参数,由于*是regex的元字符,所以要避免,或者在一个字符类中使用,因此可行的方法就是:
text = text.replaceAll("[*]",""); // OR
text = text.replaceAll("\\*","");