How to replace string in Groovy

Groovy

Groovy 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.

References

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBilow YuriyView Question on Stackoverflow
Solution 1 - GroovybaaoView Answer on Stackoverflow
Solution 2 - Groovyskia.heliouView Answer on Stackoverflow