StringUtils - 在同一行替换不同的单词
StringUtils - Replace on the same line different words
我读到使用 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);
我读到使用 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);