Java StringUtils-替换同一行中的不同单词

Java StringUtils-替换同一行中的不同单词,java,apache-stringutils,Java,Apache Stringutils,我读到使用String.replaceAll非常慢。这是我以前的做法: String settence = "Java /*with*/ lambda /*expressions*/"; String replaceSettence = settence.replaceAll(Pattern.quote("/*with*/"), "is").replaceAll(Pattern.quote("/*expressions*/"), "future"); System.out.println(re

我读到使用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 settence = "Java /*with*/ lambda /*expressions*/";
String replaceSettence = StringUtils.replaceEach(settence, new String[]{"/*with*/", "/*expressions*/"}, new String[]{"is","future"});
System.out.println(replaceSettence);
就这样,

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

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


你在哪里读到的
字符串。replace
非常慢?@Reimeus,没错!!Goldbones你能提供你读到的源代码吗?你不能仅仅用每个函数的一个调用来结束。你必须用不同的关键字/句子进行多次迭代,并计算平均时间。你在哪里读到的
字符串。replace
非常慢?@Reimeus,没错!!Goldbones你能提供你读到的源代码吗?你不能仅仅用每个函数的一个调用来结束。你必须用不同的关键字/句子进行多次迭代,并计算平均时间。你在哪里读到的
字符串。replace
非常慢?@Reimeus,没错!!Goldbones你能提供你读到的源代码吗?你不能仅仅用每个函数的一个调用来结束。你必须用不同的关键字/句子进行多次迭代,并计算平均时间。你在哪里读到的
字符串。replace
非常慢?@Reimeus,没错!!Goldbones你能提供你读到的源代码吗?你不能仅仅用每个函数的一个调用来结束。您必须使用不同的关键字/句子进行多次迭代,并计算平均时间。是否有任何方法来替换单词,例如text:“Princeton Group,Pa”=>key:“inc”,value:“这将替换子字符串而不是单词是的,您可以使用正则表达式中的空格来捕获单词。或者将你的句子按空格分割,你将有一个单词数组,这样你就可以替换你想要的。有没有办法替换单词,例如text:“Princeton Group,Pa”=>key:“inc”,value:“这将替换子字符串而不是单词是的,你可以在正则表达式中使用空格来捕捉单词。或者将你的句子按空格分割,你将有一个单词数组,这样你就可以替换你想要的。有没有办法替换单词,例如text:“Princeton Group,Pa”=>key:“inc”,value:“这将替换子字符串而不是单词是的,你可以在正则表达式中使用空格来捕捉单词。或者将你的句子按空格分割,你将有一个单词数组,这样你就可以替换你想要的。有没有办法替换单词,例如text:“Princeton Group,Pa”=>key:“inc”,value:“这将替换子字符串而不是单词是的,你可以在正则表达式中使用空格来捕捉单词。或者把你的句子按空格分开,你会有一个单词数组,这样你就可以替换你想要的。