Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache StringUtils vs Java implementation of replace()

What would be the difference between Java 1.4.2's implementation of replace, and Apache 2.3's implementation? Is there a performance gain one over another?

Java 1.4.2 replace

Apache 2.3 replace

like image 525
Oh Chin Boon Avatar asked Aug 10 '11 13:08

Oh Chin Boon


People also ask

What is the difference between replaceAll and replace in Java?

The difference between replace() and replaceAll() method is that the replace() method replaces all the occurrences of old char with new char while replaceAll() method replaces all the occurrences of old string with the new string.

What is the use of StringUtils in Java?

The StringUtils class defines certain words related to String handling. StringUtils handles null input Strings quietly. That is to say that a null input will return null . Where a boolean or int is being returned details vary by method.

Is string replace costly in Java?

As you know, in Java, Strings are immutable. So every time we construct or concatenate a String object, Java creates a new String – this might be especially costly if done in a loop.


2 Answers

The String.replace() method you linked to takes two char values, so it only ever replaces on character with another (possibly multiple times, 'though).

The StringUtils.replace() method on the other hand takes String values as the search string and replacement, so it can replace longer substrings.

The comparable method in Java would be replaceAll(). replaceAll() is likely to be slower than the StringUtils method, because it supports regular expressions and thus introduces the overhead of compiling the search string first and running a regex search.

Note that Java 5 introduced String.replace(CharSequence, CharSequence) which does the same thing as StringUtils.replace(String,String) (except that it throws a NullPointerException if any of its arguments are null). Note that CharSequence is an interface implemented by String, so you can use plain old String objects here.

like image 184
Joachim Sauer Avatar answered Sep 23 '22 10:09

Joachim Sauer


public class Compare {

    public static void main(String[] args) {
        StringUtils.isAlphanumeric(""); // Overhead of static class initialization for StringUtils
        String key = "0 abcdefghijklmno" + Character.toString('\n') + Character.toString('\r');

        String key1 = replace1(key);
        String key2 = replace2(key);
    }


    private static String replace1(String key) {
        long start = System.nanoTime();
        key = StringUtils.replaceChars(key, ' ', '_');
        key = StringUtils.replaceChars(key, '\n', '_');
        key = StringUtils.replaceChars(key, '\r', '_');
        long end = System.nanoTime() - start;
        System.out.println("Time taken : " + end);
        return key;
    }

    public static String replace2(String word) {
        long start = System.nanoTime();
        char[] charArr = word.toCharArray();

        int length = charArr.length;
        for (int i = 0; i < length; i++) {
            if (charArr[i] == ' ' || charArr[i] == '\n' || charArr[i] == '\r') {
                charArr[i] = '_';
            }
        }

        String temp = new String(charArr);
        long end = System.nanoTime() - start;
        System.out.println("Time taken : " + end);
        return temp;
    }
}

Time taken : 6400

Time taken : 5888

Times are almost the same!

I've edited the code to drop out overheads of replace2 which were not because of JDK implementation.

like image 40
sreenath V Avatar answered Sep 22 '22 10:09

sreenath V