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!
text = text. replaceAll("[*]",""); // OR text = text. replaceAll("\\*","");
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With