Search code examples
groovyjmeterjsr223

JMeter remove a single character from string variable


I am trying to remove "/" character from my specific string but I cannot succeed.

My example string:

F@S@3440642@SOW/AGT@10928415@F@-1@undefined@false

I need to remove that "/" character between "SOW" and "AGT" substrings. I tried some tricks with JSR223 but I couldn't do it; either get an error in my logs, or I cannot remove that character from the string.

Could you please help me with how to remove "/" character by using JSR223?


Solution

  • Just use String.replaceAll() function like:

    vars.put('somevar', vars.get('somevar').replaceAll('/', ''))
    

    Demo:

    enter image description here

    in the above snippet vars stands for JMeterVariables class instance, see JavaDoc for details and Top 8 JMeter Java Classes You Should Be Using with Groovy article for more information on this and other JMeter API shorthands available for the JSR223 Test Elements.


    Also be aware that there are __strReplace() and __strReplaceRegex() custom JMeter Functions so you don't need to do any Groovy scripting at all