How to replace string in Groovy
GroovyGroovy Problem Overview
I have some string like
C:\dev\deploy_test.log
I want by means of Groovy to convert string to
C:/dev/deploy_test.log
I try to perform it with command
Change_1 = Log_file_1.replaceAll('\','/');
It doesn't convert this string
Groovy Solutions
Solution 1 - Groovy
You need to escape the backslash \
:
println yourString.replace("\\", "/")
Solution 2 - Groovy
You could also use Groovy's slashy string, which helps reduce the clutter of Java's escape character \
requirements. In this case, you would use:
Change_1 = Log_file_1.replaceAll(/\/,'/');
Slashy strings also support interpolation, and can be multi-line. They're a great tool to add to your expertise.