1

Below is the function and its returning the integer values with %.

  • def functions = []
  • eval for(var i=0; i<response.functions.length; i++) functions.add(response.functions[i].value) *print functions

It's return the below output. ['2.16%','1.34%','1.32%','1.25%','0.65%','0.48%','0.42%','0.26%','0.14%','0.06%','0.03%','0%']

I want to remove the single codes and % symbol. After removing it should be like below. [2.16,1.34,1.32,1.25,0.65,0.48,0.42,0.26,0.14,0.06,0.03,0]

Please help me to remove the extra characters from Integer array

Narasimha
  • 21
  • 3

2 Answers2

0

Here you go, just do a map()

* def response = ['2.16%','1.34%','1.32%']
* def cleaned = response.map(x => x.replace('%', '') * 1)
* match cleaned == [2.16, 1.34, 1.32]

Please refer to the docs: https://github.com/karatelabs/karate#json-transforms

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
0

I am able to replace the special character, '%' in this case using below code snippet. Feel free to change the special character as required. Hope this helps.

@ReplaceChars
  Scenario: Replacing the special characters
    * def repalceValues = 
    """
        function(){
            let responseVal = ['2.16%','1.34%','1.32%','1.25%','0.65%','0.48%','0.42%','0.26%','0.14%','0.06%','0.03%','0%']
            const finalVal = new Array();
            responseVal.forEach((val) =>{
                finalVal.push(val.replace('%', ''))
            })

            karate.log("New Array : "+ finalVal)
        }
    """
    * repalceValues()