Apache StringUtils vs Java implementation of replace()

心已入冬 提交于 2019-12-01 03:19:06

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.

sreenath V
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.

1.4.2 replaces operates only with char arguments whereas the Apache 2.3 one takes in strings.

  • String.replace(char, char) can't replace whole strings
  • you can have null values with StringUtils.replace(..).

String.replace(CharSequence s1, CharSequence s2) will do the same thing if the first string is not-null. Otherwise it will throw a NullPointerException

Apache's is quite a bit faster, if I recall correctly. Recommended.

Maitrik B Panchal

To replace a string character with another string using StringUtil.Replace, I tried following and it's working fine for me to replace multiple string values from a single string.

String info = "[$FIRSTNAME$]_[$LASTNAME$]_[$EMAIL$]_[$ADDRESS$]";

String replacedString = StringUtil.replace(info, new String[] { "[$FIRSTNAME$]","[$LASTNAME$]","[$EMAIL$]","[$ADDRESS$]" }, new String[] { "XYZ", "ABC" ,"abc@abc.com" , "ABCD"});

This will replace the String value of info with newly provided value...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!