Parthasarathy K
Parthasarathy K

Reputation: 131

Replacing backslashes in Strings

Is it possible replace all backslash \\ to forward slash / in Groovy?

String someString = rootDir
someString.replace("\\","////")

This doesn't seem to work.

Upvotes: 2

Views: 1792

Answers (1)

RaGe
RaGe

Reputation: 23805

def someString = "a\\b\\c\\d/e/f/g/h"
println someString
println someString.replaceAll("\\\\", "/")

produces the output:

a\b\c\d/e/f/g/h
a/b/c/d/e/f/g/h

Note that replaceAll does not modify the string in place, but returns a new modified string.

Upvotes: 3

Related Questions