Replace spaces with %20 in device names in groovy?

Is there a way to replace spaces in a device’s display name with %20 for purposes of including them in an http string?

For example, I have a device called “Kitchen Light”. Accordingly if I do something like

log.trace "${device.displayName}"

The simulator will correctly return: Kitchen Light. But what I want is Kitchen%20Light. This way I can drop that into a url. If I do it “as is” I get an error because of the space.

1 Like
def toReplace = device.displayName
def replaced = toReplace.replaceAll(' ', '%20')
log.debug replaced
1 Like

Perfect!! Thanks much @scottinpollock