Usually, no one has to know this. It is the year 2011, though i am really happy to be involved in a big project, but being enterprise lately seem to mean going 1.4.2. Right, that is the version of COBOL Java that most enterprise applications are still running on. I wish there were like a Y2K time bomb for users of 1.4.2, but it isn’t going to happen so anyway …

Say you have like a task to get rid of certain character and replace it with an empty String, what are your options?

Here’s the tricky part,

1. Are you on JDK 1.5 (or JavaSE 5 or Tiger) and above?

2. Can you use third party APIs such as Apache Commons Lang?

I discuss first the caveat – where you are not on JDK 1.5 and above, and you can’t use third party APIs.


JDK 1.4.2 String class’s replace(oldChar: char, newChar: char): String

Remember, this option is prevalent to you if you are on JDK 1.4.2 (i am not so sure if there will ever be a 1.4.3, but assuming all is sane …) and below AND you can’t use an external party API, such as Apache Commons Lang.

This method takes in a character argument (oldChar) and replaces it with another character (newChar) argument. So here is the caveat, you must replace a character with another character!

For example:

(new String("abcabc")).replace('a', 'n'); // returns "nbcnbc"
(new String("abcabc")).replace('v', 'x'); // returns "abcabc" 
(new String("abcabc")).replace('b', ''); // compile error because there is no "empty character" character
(new String("abcabc")).replace('b', 'burger'); // compile error because this is not a character!

So if you looking to solve a situation of replacing a certain character with an empty character, you won’t find an elegant solution with replace (this is for JDK 1.4.2 and below).

Note that you probably have also heard about the replaceAll method, and you would think that replace is replacing a single occurrence of the matching character, while replaceAll is replacing all occurrences of the matching character, well it is not, both replace and replaceAll replace all occurrences of the matching character/ regex respectively. More on this to come in the later part of the post.


1. You may only replace one character with another character

2. An “empty string” is not a character

JDK 5 and above String class’s overloaded replace(target: CharSequence, replacement: CharSequence): String

This method is only since the introduction of Java 1.5.

This method takes in an interface CharSequence as an argument for both its target and replacement, but FEAR not, i know what you must be thinking right now! No, CharSequence is one of the interfaces that Java String class implements. So, providing String as arguments to this method is 99% of the time what we want out of it.

For brevity, because String implements CharSequence, you can be expected to provide String as argument to this method.

(new String("abcabc")).replace("a", "n"); // returns "nbcnbc"
(new String("abcabc")).replace("v", "x"); // returns "abcabc" 
(new String("abcabc")).replace("b", ""); // returns "acac"
(new String("abcabc")).replace("b", "burger"); // returns "aburgercaburgerc"

I am quite tempted to write something about character sequence and String but i guess this will have to come later!

String class’s replaceAll(regex: String, replacement: String): String

Right from JDK 1.4.2, Java has provided a regex (regular expression) based search and replace method for all your technically possible scenarios.

This method is powerful in that it is less convenient to use, easily mistaken, and always abused!

(new String("abcabc")).replaceAll("a", "n"); // returns "nbcnbc" but be careful its regex!
(new String("abcabc")).replaceAll("v", "x"); // returns "abcabc" but be careful its regex!
(new String("abcabc")).replaceAll("b", ""); // returns "acac"
(new String("")).replaceAll(".", "ad"); // returns something you least expect, its REGEX!
(new String("abcabc")).replaceAll("b", "burger"); // returns "aburgercaburgerc"

Notice that replaceAll is really regex, it does not mean to replace all occurrence of matches (even so it DOES). So doing a replaceAll(“.”, “ad”) will give you least expected results. : >

(new String("")).replaceAll(Pattern.quote(".", "ad"); // returns "abcadabc"

More examples:

(new String("abc|abc")).replaceAll(Pattern.quote("|", "@"); // returns "abc@abc"
(new String("abc|abc")).replaceAll("\\|", "@"); // returns "abc@abc"
(new String("abc|abc")).replaceAll("|", "@"); // remember, its REGEX : >

Apache Commons Lang StringUtils.replace(text: String, searchString: String, replacement: String): String

As easy as it may get, as always from Apache. ( :

Similarly, the Apache Commons Lang StringUtils replace method replaces all occurrences of the target String, and faster. (Performance testing results in a while)

StringUtils.replace("abcabc", "a", "n"); // returns "nbcnbc"
StringUtils.replace("abcabc", "v", "x"); // returns "abcabc" 
StringUtils.replace("abcabc", "b", ""); // returns "acac"
StringUtils.replace("abcabc", "b", "burger"); // returns "aburgercaburgerc"
StringUtils.replace("abc|abc", "|", "@"); // returns "abc@abc"

Here are some quick performance test results, Apache Commons Lang StringUtils.replace is a winner. (I hope you were not hoping for something else… Apache Commons’s the Mama Lemon / AJAX washer to your conventional soap)

300, 000, 000 iterations on a single thread

Gen 1 Core i5 520M (Mobile Processor)

64 bit Windows OS

64 bit JVM JRockit


2, 100, 000, 000 iterations on a single thread

Intel Pentium D -____-

32 bit Windows OS

32 bit JVM Sun HotSpot


Here are the source codes you may download to give a try.

That is all folks!



Source code 2KB java-concepts-talk Google Code Host