I have a string which contain a directory path. Value of the string is set from some environment variable. The number of slashes in the string might vary like below (as in sysout).
String path = "C:\dirA\dirB\dirC"
or
String path = "C:\\dirA\\dirB\\dirC"
I am expecting \\ and not \. For this, I can try path.replace('\','\\') which converts first string to second one. But if the environment variable is already in the format of C:\\dirA\\dirB\\dirC, then path.replace() will give me C:\\\\dirA\\\\dirB\\\\dirC, and I dont want that. So how can I replace \ with \\ and leave the existing \\ in the string?
You can use path.replaceAll('\\\\+','\\\\\\\\')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With