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

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

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest