Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java replace method, replacing with empty character [duplicate]

Tags:

java

string

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?

like image 288
capcom Avatar asked Nov 03 '12 14:11

capcom


2 Answers

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.

like image 118
Marko Topolnik Avatar answered Oct 03 '22 03:10

Marko Topolnik


try :

string.replace(".", ""); 

Other way is to use replaceAll :

string.replaceAll("\\.",""); 
like image 32
Grisha Weintraub Avatar answered Oct 03 '22 04:10

Grisha Weintraub