如何在Beanshell中使用Split for string?

Sha*_*han 0 split jmeter beanshell

我正在JMeter中编写一个Beanshell采样器来读取一个文件,我希望用逗号(,)分隔每行,并想要提取值.我得到了第一个带有索引0的分裂字符串,但对于索引为1,2的下一个值,所以......它没有给出值.

value = value.append(line.split(",")[2]);

在这里,对于索引0,它对索引大于0的所有精细BUT都工作,它失败了.

Dmi*_*i T 6

你确定你的line.split(",")表达式产生长度> 1的String数组吗?你怎么知道的?

例如下一个代码:

String line = "quick, brown, fox, jumped, over";
String[] words = line.split(",");
for (int i = 0; i < words.length; i++) {
  log.info(words[i]);
  if (i == 2) {
      log.info("Third word is: " + words[2]);
  }
}
Run Code Online (Sandbox Code Playgroud)

产生以下输出:

2014/10/08 13:17:37 INFO  - jmeter.util.BeanShellTestElement: quick 
2014/10/08 13:17:37 INFO  - jmeter.util.BeanShellTestElement:  brown 
2014/10/08 13:17:37 INFO  - jmeter.util.BeanShellTestElement:  fox 
2014/10/08 13:17:37 INFO  - jmeter.util.BeanShellTestElement: Third word is:  fox 
2014/10/08 13:17:37 INFO  - jmeter.util.BeanShellTestElement:  jumped 
2014/10/08 13:17:37 INFO  - jmeter.util.BeanShellTestElement:  over 
Run Code Online (Sandbox Code Playgroud)

我建议使用通过log.info方法记录所有值并检查输出.由于JMeter 2.6 Log Viewer可用,您应该能够在JMeter GUI中看到正在发生的事情:

Beanshell记录

有关更多Beanshell提示和技巧,请参阅如何使用BeanShell:JMeter最喜欢的内置组件指南.