1

Hello I have a string like a= " $ 2 187.00" . I tried removing all the white spaces and the bad characters like a.replaceAll("\\s","").replace("$","") . but i am getting error Impossible to parse JSON response: SyntaxError: JSON.parse: bad escaped character how to remove the bad character in this expression so that the value becomes 2187.00.Kindly help me .Thanks in advance

shashank
  • 379
  • 5
  • 6
  • 15

1 Answers1

4
def a = ' $ 2 187.00'
a.replaceAll(/\s/,"").replaceAll(/\$/,"")

// or simply
a.replaceAll(/[\s\$]/,"")

It should return 2187.00.

Note

  1. that $ has special meaning in double quoted strings literals "" , called as GString.
  2. In groovy, you can user regex literal, using that is better than using regex with multiple escape sequences in string.
kdabir
  • 9,623
  • 3
  • 43
  • 45
  • Thanks kunal it worked how to convert the result to numeric please help me i am stuck.Thank you – shashank Aug 10 '13 at 07:29
  • Just add `as Double` to end of expression, the string will be converted to Double (or float if you want) – kdabir Aug 10 '13 at 07:31
  • Hey kunal i tried but i am getting following error groovy.lang.MissingPropertyException: No such property: asDouble for class: java.lang.String at Script1.run(Script1.groovy:2) i tried a.replaceAll(/\s/,"").replaceAll(/\$/,"").asDouble please help me – shashank Aug 10 '13 at 07:35