Android. Replace * character from String

后端 未结 3 870
时光取名叫无心
时光取名叫无心 2021-01-04 04:07

I have a String variable that contains \'*\' in it. But Before using it I have to replace all this character.

I\'ve tried replaceAll function but without success:

3条回答
  •  借酒劲吻你
    2021-01-04 04:20

    Why not just use String#replace() method, that does not take a regex as parameter: -

    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. So, your way of doing it would be: -

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

    But, you really can use simple replace here.

提交回复
热议问题