本文介绍了如何在Jmeter中使用bean shell逐字符替换白色字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个变量,即"Classification",其值为"Vol 4",我需要在"Classification"变量中用"+"字符替换空格.因此,我需要在bean shell中编写一个脚本,以逐字符替换空白字符.
I have a variable i.e, "Classification" and it`s value is "Vol 4" and I needs to replace white space with "+" character in "Classification" variable. So, I required to write a script in bean shell to replace white space character by character.
有人可以帮我吗?
推荐答案
我认为您正在寻找URL编码功能.有2种方法可供选择:
I think that you're looking for URL encoding function. There are 2 ways to go:
- 简单-使用JMeter的功能对URL进行编码.假设您具有分类"变量,它将类似于
${__urlencode(${Classification})}
- 更强(但更灵活)的一个-使用Beanshell来做同样的事情.
示例Beanshell代码:
Example Beanshell code:
import java.net.URLEncoder; // import required class
String classification = vars.get("Classification").toString(); // obtain value of "Classification" variable
String encoded = URLEncoder.encode(classification, System.getProperty("file.encoding")); // encode "Classification variable and save result to "encoded" string
vars.put("Classificatoin",encoded); // replace "Classification" variable with new value
如果您只需要替换单个或多个字符,请参阅先前答案中的解决方案.
If you need just replacing single or multiple characters - see the solution is in previous answer.
参考文献:
- __urlEncode() JMeter函数
- 如何使用Beanshell 指南
- 字符串 Java类文档
- __urlEncode() JMeter Function
- How to use Beanshell guide
- String Java class documentation
这篇关于如何在Jmeter中使用bean shell逐字符替换白色字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!