String s ="[ABC_JJBB_DD]";
I'm getting string value from application data, how could I remove special character [ and ] in the string in groovy. Using
s.removeAll('[','']
String s ="[ABC_JJBB_DD]";
I'm getting string value from application data, how could I remove special character [ and ] in the string in groovy. Using
s.removeAll('[','']
You can do it the java route, as Aniket says:
s.substring(1, s.length()-1)
Or you can take the Groovy route of:
s[1..-2]
But you'd need to ensure s is longer than 1 char, with both of these, or else you'll get a StringIndexOutOfBoundsException
If you don't want to check, you can do esoteric things like
s.drop(1).reverse().drop(1).reverse()
Which will work, but will be slower
Or, if you just want to remove all [ and ], you could do
s.replaceAll(~/[\[\]]/, '')
Since Groovy 3 you can use takeBetween, that has beside other arguments one for start and end string. E.g.
groovy:000> "[ABC_JJBB_DD]".takeBetween("[","]")
===> ABC_JJBB_DD