75

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

baao
  • 71,625
  • 17
  • 143
  • 203
Bilow Yuriy
  • 1,239
  • 3
  • 13
  • 20

2 Answers2

151

You need to escape the backslash \:

println yourString.replace("\\", "/")
baao
  • 71,625
  • 17
  • 143
  • 203
  • 1
    I am using like, uname1 = uname.replace('@','%40') But getting error in jenkins pipeline.. groovy.lang.MissingMethodException: No signature of method: hudson.util.Secret.replace() is applicable for argument types: (java.lang.String, java.lang.String) Can you help – Surya Jul 09 '21 at 10:42
  • 1
    @SuryaN Looks like you're trying to call this method on a credential value. Pipeline casts credentials in a class hudson.util.Secret, which doesn't have this method. You can get a String object from Secret by calling getPlainText() method on it. – MaroonedMind Sep 10 '21 at 07:33
3

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

nikodaemus
  • 1,918
  • 3
  • 21
  • 32