8 Replies Latest reply: Apr 3, 2009 12:12 PM by 807588

# How to replace \ with \\ in java String?

Hi
I have java String as c:\MyFolder\abc\songs

I want to replace \ with \\
New string should be as - c:\\MyFolder\\abc\\songs

How to achieve this?
Why?
• ###### 2. Re: How to replace \ with \\ in java String?
String#replaceAll(...)

and again... why?
• ###### 3. Re: How to replace \ with \\ in java String?
If you are reading this in from some properties file or some other external resource and you need to escape it so that Java doesn't complain then you should consider making your file path platform independent.

``c:/MyFolder/abc/songs``
• ###### 4. Re: How to replace \ with \\ in java String?
binary_coder wrote:
If you are reading this in from some properties file or some other external resource and you need to escape it so that Java doesn't complain then you should consider making your file path platform independent.

``c:/MyFolder/abc/songs``
"c:/" platform independent?
• ###### 5. Re: How to replace \ with \\ in java String?
sabre150 wrote:
binary_coder wrote:
If you are reading this in from some properties file or some other external resource and you need to escape it so that Java doesn't complain then you should consider making your file path platform independent.

``c:/MyFolder/abc/songs``
"c:/" platform independent?
Yes, it works in any version of Windows.
• ###### 6. Re: How to replace \ with \\ in java String?
sabre150 wrote:
binary_coder wrote:
If you are reading this in from some properties file or some other external resource and you need to escape it so that Java doesn't complain then you should consider making your file path platform independent.

``c:/MyFolder/abc/songs``
"c:/" platform independent?
Yes, it works in any version of Windows.
I'll have to dig out my 15 3.5" floppies* and install 3.1 to know for sure, but I suspect that's not true.

*I tried to get people to call them "stiffies" (since they aren't floppy), but that never caught on
• ###### 7. Re: How to replace \ with \\ in java String?
sabre150 wrote:
binary_coder wrote:
If you are reading this in from some properties file or some other external resource and you need to escape it so that Java doesn't complain then you should consider making your file path platform independent.

``c:/MyFolder/abc/songs``
"c:/" platform independent?
No, I should have emphasized more on the forward slashes bit. Glad to see I caught my PedanticBallSnifferException. :-)
• ###### 8. Re: How to replace \ with \\ in java String?
binary_coder wrote:
sabre150 wrote:
binary_coder wrote:
If you are reading this in from some properties file or some other external resource and you need to escape it so that Java doesn't complain then you should consider making your file path platform independent.

``c:/MyFolder/abc/songs``
"c:/" platform independent?
No, I should have emphasized more on the forward slashes bit. Glad to see I caught my PedanticBallSnifferException. :-)
I always try to live up/down to expectations.