gpt4 book ai didi

java - 拥有一个字符串,替换然后执行拆分或拥有一组字符串并创建一个新的字符串来更改它会更有效吗?

转载 作者:行者123 更新时间:2023-12-01 16:36:02 25 4
gpt4 key购买 nike

在为 ProcessBuilder.command 方法准备参数时,我尚未决定应该做什么。方法 1 肯定更快,但我在这里寻找最好的效率。

方法一:

String test = "ls | grep $name -option";
test = test.replace("$name",textField.getText());
new ProcessBuilder.command(test.split(" "));

方法2:

String[] test = { "ls", "|", "grep", "$name", "-option" };
String[] newTest = { test[0], test[1], test[2], textField.getText(), test[4] };
new ProcessBuilder.command(newTest);

最佳答案

第二种方法在整体性能上会更高效(因为replace方法实现和split方法调用)。

但在这种情况下,执行时间不会有显着差异。

关于java - 拥有一个字符串,替换然后执行拆分或拥有一组字符串并创建一个新的字符串来更改它会更有效吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61944814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com