我读到使用 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));