I have a file name which is stored in a String variable path.
I tried this:
path = path.replaceAll('\','/')
but it does not work.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
replaceAll()needsStringsas parameters.So, if you write
it fails because you should have written
But this also fails because character ‘\’ should be typed ‘\\’.
And this will fail during execution giving you a
PatternSyntaxException, because the fisrStringis a regular expression (Thanks @Bhavik Shah for pointing it out). So, writing it as a RegEx, as @jlordo gave in his answer:Is what you were looking for.
To make optimal your core, you should make it independent of the Operating System, so use @Thai Tran’s tip:
But this fails throwing an
StringIndexOutOfBoundsException(I don’t know why). It works if you usereplace()with no regular expressions: