android,替换字符串中*字符

有一个字符串变量,包含*。但是我需要替换其中所以的*字符。

我写的代码没有实现:

text = text.replaceAll("*","");
text = text.replaceAll("*",null);

请大家帮忙解决,谢谢。

String#replace() 方法实现多简单,这个方法不会将regex当做参数。

text = text.replace("*","");

相反的,String#replaceAll() 将regex当做第一个参数,由于*是regex的元字符,所以要避免,或者在一个字符类中使用,因此可行的方法就是:

text = text.replaceAll("[*]","");  // OR
text = text.replaceAll("\\*","");

要注意转义符,类似这样

String str = "12334*ipi*kp";        
System.out.println(str.replaceAll("\\*", "A"));

有些符号需要转义:如*和.