Suppose I have the following in String
format:
2.2
And I want to replace the decimal point with an empty space, to make it look like this:
22
How do I do this? I thought replace
would have done the trick, but when I try it like this:
string.replace('.', '');
I get an error with the ''
because it supposedly isn't a character. That makes sense, so how else can I accomplish what I want?
If you just exchange single for double quotes, this will work because an empty string is a legal value, as opposed to an "empty character", and there's an overload replace(CharSequence, CharSequence)
. Keep in mind that CharSequence
is the supertype of String
.
try :
string.replace(".", "");
Other way is to use replaceAll
:
string.replaceAll("\\.","");
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