在为 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/