[英]Android. Replace * character from String
I have a String variable that contains '*' in it. 我有一个String变量,其中包含'*'。 But Before using it I have to replace all this character.
但在使用之前我必须更换所有这个角色。
I've tried replaceAll function but without success: 我尝试过replaceAll函数,但没有成功:
text = text.replaceAll("*","");
text = text.replaceAll("*",null);
Could someone help me? 有人能帮助我吗? Thanks!
谢谢!
Why not just use String#replace()
method, that does not take a regex
as parameter: - 为什么不使用
String#replace()
方法,不使用regex
作为参数: -
text = text.replace("*","");
In contrary, String#replaceAll()
takes a regex as first parameter, and since *
is a meta-character in regex, so you need to escape it, or use it in a character class. 相反,
String#replaceAll()
将正则表达式作为第一个参数,因为*
是正则表达式中的元字符 ,所以您需要将其转义,或者在字符类中使用它。 So, your way of doing it would be: - 所以,你这样做的方式是: -
text = text.replaceAll("[*]",""); // OR
text = text.replaceAll("\\*","");
But, you really can use simple replace here. 但是,你真的可以在这里使用简单的替换 。
you can simply use String#replace() 你可以简单地使用String#replace()
text = text.replace("*","");
String.replaceAll(regex, str) takes regex as a first argument, as *
is a metachacter you should escape it with a backslash to treat it as a normal charcter. String.replaceAll(regex,str)将正则表达式作为第一个参数,因为
*
是一个元变量,你应该用反斜杠转义它以将其视为普通的字符。
text.replaceAll("\\*", "")
Try this. 试试这个。
You need to escape the *
for the regular expression, using . 您需要使用转义正则表达式的
*
。
text = text.replaceAll("\\*","");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.