How to replace string in Groovy

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

2 Answers

You need to escape the backslash \:

println yourString.replace("\\", "/") 
2

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.

References

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like