我读到使用String.replaceAll非常慢。这是我的旧方法:

String settence = "Java /*with*/ lambda /*expressions*/";
String replaceSettence = settence.replaceAll(Pattern.quote("/*with*/"), "is").replaceAll(Pattern.quote("/*expressions*/"), "future");

System.out.println(replaceSettence);


如何用StringUtils做到这一点?

编辑:

感谢您的快速解答。

我使用Steph提供的代码。我的结论是replaceAll必须比StringUtils更快:

  Long startTime = System.currentTimeMillis();
        String settence = "Java /*with*/ lambda /*expressions*/";
        String replaceSettence = settence.replaceAll(Pattern.quote("/*with*/"), "is").replaceAll(Pattern.quote("/*expressions*/"), "future");

        System.out.println(replaceSettence + " " + (System.currentTimeMillis() - startTime));

        String settenceStringUtils = "Java /*with*/ lambda /*expressions*/";
        String replaceStringUtils = StringUtils.replaceEach(settence, new String[]{"/*with*/", "/*expressions*/"}, new String[]{"is", "future"});
        System.out.println(replaceStringUtils + "StringUtils-> " + (System.currentTimeMillis() - startTime));

最佳答案

像这样:

String settence = "Java /*with*/ lambda /*expressions*/";
String replaceSettence = StringUtils.replaceEach(settence, new String[]{"/*with*/", "/*expressions*/"}, new String[]{"is","future"});
System.out.println(replaceSettence);

10-07 16:38