Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace all occurrences of a character in a string in java? [duplicate]

Tags:

java

A very noob question..

I want to replace all the occurance of "." in a string with empty space..

So this is what I tried

             String s = "1.2.3.4";
        System.out.println(s);
         s = s.replaceAll(".", " ");
        System.out.println(s);

But the second print is an empty print?

What did i missed here?

like image 337
frazman Avatar asked Oct 25 '13 20:10

frazman


2 Answers

You want to escape the .. Otherwise, it can match anything.

Try s.replaceAll("\\.", " ") instead.

like image 98
Terry Li Avatar answered Nov 09 '22 10:11

Terry Li


Use String.replace(char, char) instead of String.replaceAll

like image 28
Evgeniy Dorofeev Avatar answered Nov 09 '22 10:11

Evgeniy Dorofeev