Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android. Replace * character from String

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:

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

Could someone help me? Thanks!

like image 222
Viktor K Avatar asked Jan 29 '13 19:01

Viktor K


People also ask

How can I replace one character in a String in Android?

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

How do you change special characters on Android?

static String replaceString(String string) { return string. replaceAll("[^A-Za-z0-9 ]","");// removing all special character. } this is work great but if user will enter the other language instead of eng then this code will replace the char of other language.

How do I replace a character in a String?

The Java string replace() method will replace a character or substring with another character or string. The syntax for the replace() method is string_name. replace(old_string, new_string) with old_string being the substring you'd like to replace and new_string being the substring that will take its place.


1 Answers

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.

like image 182
Rohit Jain Avatar answered Sep 25 '22 02:09

Rohit Jain