![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2006
Posts: 15
Rep Power: 0
![]() |
"\"
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.
__________________
What? Outside??? I have heard of that thing before, but I can't remember the details. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
Wow. Deja Vu. In almost all programming languages, the string "\\" represents a literal backslash.
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jun 2006
Posts: 15
Rep Power: 0
![]() |
-_-
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?
__________________
What? Outside??? I have heard of that thing before, but I can't remember the details. Last edited by Xian_Fung; Dec 12th, 2006 at 6:17 PM. Reason: Don't like explination |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 4
![]() |
As I said, "\\" represents a "\". So in order to replace "\" with "/", you'll need code that looks like this:
dir.replace("\\", "/"); |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Jun 2006
Posts: 15
Rep Power: 0
![]() |
Ok. One problem with your solution though, I tried that earlier and it didn't work.
__________________
What? Outside??? I have heard of that thing before, but I can't remember the details. |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 "\\\\".
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Jun 2006
Posts: 15
Rep Power: 0
![]() |
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.
__________________
What? Outside??? I have heard of that thing before, but I can't remember the details. Last edited by Xian_Fung; Dec 12th, 2006 at 8:20 PM. Reason: Looks wrong |
|
|
|
|
|
#8 | |
|
Hobbyist Programmer
|
Did you even try Dawei's solution?
Quote:
__________________
Pain is just weakness leaving the body.
|
|
|
|
|
|
|
#9 |
|
Newbie
Join Date: Jun 2006
Posts: 15
Rep Power: 0
![]() |
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 "\\".
__________________
What? Outside??? I have heard of that thing before, but I can't remember the details. |
|
|
|
|
|
#10 |
|
Expert Programmer
|
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("\\\\", "/"); |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| solved - Error before token "{" | Daniel_kd | C | 3 | Apr 15th, 2005 12:00 PM |