Android系统。从String中替换*字符 [英] Android. Replace * character from String
问题描述
我有一个包含'*'的String变量。但在使用它之前我必须替换所有这些字符。
I have a String variable that contains '*' in it. But Before using it I have to replace all this character.
我尝试过replaceAll函数但没有成功:
I've tried replaceAll function but without success:
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: -
text = text.replace("*","");
相反,String# replaceAll()
将正则表达式作为第一个参数,因为 *
是一个元字符正则表达式,所以你需要逃避它,或在字符类中使用它。所以,你这样做的方法是: -
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. So, your way of doing it would be: -
text = text.replaceAll("[*]",""); // OR
text = text.replaceAll("\\*","");
但是,你真的可以在这里使用简单的替换。
But, you really can use simple replace here.
这篇关于Android系统。从String中替换*字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!