shashank
shashank

Reputation: 399

Syntax error in removing bad character in groovy

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

Upvotes: 1

Views: 516

Answers (1)

kdabir
kdabir

Reputation: 9868

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.

Upvotes: 4

Related Questions