![]() |
"\"
I'm trying to replace occurances of "\" to "/" in a string containing a directory. Unfortunately java expects an escape character to go with it and throws off what it believes to be a string, if that makes any sense.
Doesn't work of course --> dir.replace("\", "/"); I do have a sad, but functional solution. I wanted to know if there are any better ways. -Thank you. |
Wow. Deja Vu. In almost all programming languages, the string "\\" represents a literal backslash.
|
-_-
Yes, but the problem is that I cannot get Java to identify the "\" in the code and string. The compiler throws a build error because the backslash throws off what is identified as a string in the code. So trying to specifically identify the "\" by itself cannot be done. The purpose of this part of code is to revert any "\" to "/". I knew I didn't explain to well. Besides, Mysql takes "\" per pair out of the string because it thinks it's an escape character (along those lines).
dir.replace("\", "/"); from ", "/"); is seen as a string (except "/") in my code. any better? |
As I said, "\\" represents a "\". So in order to replace "\" with "/", you'll need code that looks like this:
:
dir.replace("\\", "/"); |
Ok. One problem with your solution though, I tried that earlier and it didn't work.
|
If the "\\", which represents '\', is in a string that gets passed to another app (like MySQL) that interprets it as an escape character, then you need "\\\\".
|
Ok, back to the beginning. This section of the program is going to read old directories used by the user. If the user decides to change the directory, that new directory goes into the database. Before entering it into the database, I want it to change the "\" to "/". Now, here lies the problem. While coding, the Java compiler connot simply find a "\". It interperates it as an escape character "\", not the actual character. All those solutions are valid and I thank you very much, but it doesn't strike the problem. Instead, they're doing it after-the-fact of FINDING the "\". The whole point is that the program needs to find "\" 's in a string and change them to "/". As I said, it's being ABLE to code the program to find the "\" without it thinking it's an escape character.
Ex: (Please excuse the periods, forums doesn't allow empty spaces. Imagine empty space is there and not periods.) dir.replace("\", "/"); |________| |____| .......|.............| .......|.............| ------(all in string according to the compiler)(errors here) .......|........................(b/c the "\" appears as an escape character setup) (Normal coding) This is only one example to represent what will happen in other string manipulations. I'm terrible at explaining things, sorry. I hope I explained it better this time. |
Did you even try Dawei's solution?
Quote:
|
I'm sorry. I don't know what happened with my compiler because I tried it before and it didn't work at all. Of course, after all this, it works now- using "\\".
|
Note, first of all, that according to the Java docs, the String.replace function takes characters, not Strings. (I am not sure why passing Strings rather than characters seems to work.) Passing an escaped backslash character will solve your problem:
:
p = p.replace('\\', '/');Alternatively, the String.replaceAll function treats its first parameter as a regular expression, and as such (as DaWei said), you must escape the backslash in the regular expression: :
p = p.replaceAll("\\\\", "/"); |
| All times are GMT -5. The time now is 5:57 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC