How to Replace Comma with Space in Java

Learn about how to replace comma with space in java.

Replace comma with space in java

1. Using replace() method

Use String’s replace() method to replace comma with space in java.

Here is syntax of replace() method:

Output:

1 2 3 4

As you can see, replace() method replaced each comma with space in the String.

2. Using replaceAll() method

Use replaceAll() method to replace comma with space in java. It is identical to replace() method, but it takes regex as argument. You can go through difference between replace and replaceAll over here.

Here is syntax of replaceAll method:

Output:

1 2 3 4

That’s all about how to replace comma with space in java.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *