I have a string like “C:\Program Files\Directory\Something.txt” and I would like to convert it into “C:\\Program Files\\Directory\\Something.txt” So basically add \ wherevever a \ is found. Is the best way to approach this using indexOf and breaking the string into sustrings and then concatenating again or is there a more efficent way of doing this in Java?
I have a string like C:\Program Files\Directory\Something.txt and I would like to convert it
Share
[Edit]
If the string has backslashes then it gets really fun since that is the regular expression escape character. This should work:
Note that’s four backslashes in the regex (two pairs in sequence to get two literal backslashses) and then eight in the replacement string since backslashes are also escape characters for literal strings.