Apache StringUtils 与 Replace() 的 Java 实现

发布于 2024-11-28 15:13:08 字数 417 浏览 0 评论 0原文

Java 1.4.2 的替换实现和 Apache 2.3 的实现之间有什么区别?两者之间是否存在性能提升?

Java 1.4 .2 替换

Apache 2.3 替换

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

从此见与不见 2024-12-05 15:13:09

您链接到的 String.replace() 方法采用两个 char 值,因此它只会用另一个字符替换一个字符(尽管可能多次)。

另一方面,StringUtils.replace() 方法将 String 值作为搜索字符串和替换值,因此它可以替换较长的子字符串。

Java 中的类似方法为 replaceAll()replaceAll() 可能比 StringUtils 方法慢,因为它支持正则表达式,因此引入了首先编译搜索字符串并运行正则表达式搜索的开销。

请注意,Java 5 引入了 String.replace(CharSequence, CharSequence) 其作用与StringUtils.replace(String,String)(除非它的任何参数为 null,否则会抛出 NullPointerException)。请注意,CharSequence 是由 String 实现的接口,因此您可以在此处使用普通的旧 String 对象。

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.

鲜肉鲜肉永远不皱 2024-12-05 15:13:09
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;
    }
}

花费时间:6400

耗时:5888

时间几乎相同!

我编辑了代码以删除 replace2 的开销,这不是由于 JDK 实现造成的。

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.

奶茶白久 2024-12-05 15:13:09

1.4.2 替换了仅使用 char 参数进行的操作,而 Apache 2.3 则接受字符串。

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

半寸时光 2024-12-05 15:13:09
  • String.replace(char, char) 无法替换整个字符串,
  • 您可以使用 StringUtils.replace(..) 获取 null 值。

如果第一个字符串不为空,String.replace(CharSequence s1, CharSequence s2) 将执行相同的操作。否则它会抛出一个NullPointerException

  • 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

相权↑美人 2024-12-05 15:13:09

如果我没记错的话,Apache 的速度要快得多。受到推崇的。

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

太阳男子 2024-12-05 15:13:09

要使用 StringUtil.Replace 将字符串字符替换为另一个字符串,我尝试了以下操作,并且它可以很好地从单个字符串替换多个字符串值。

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

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

这将用新提供的值替换 info 的字符串值...

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" ,"[email protected]" , "ABCD"});

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文