Java : Remove all occurrences of char from string

So if you wish to remove all occurences of a certain char from a string you just need to use the String in-built function  – replace(char,emptyChar).

For example, I wish to replace all occurences of ‘:’ from

String str = “bc:14:01:b0:3a:b8”;

To do so I replace the char ‘:’ with the empty one “” like this:

str = str.replace(“:”, “”);

And I get the desired output:

bc1401b03ab8

Yup, Is this simple ;).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s