Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 12th, 2006, 5:24 PM   #1
Xian_Fung
Newbie
 
Join Date: Jun 2006
Posts: 15
Rep Power: 0 Xian_Fung is an unknown quantity at this point
"\"

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.
Xian_Fung is offline   Reply With Quote
Old Dec 12th, 2006, 5:42 PM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Wow. Deja Vu. In almost all programming languages, the string "\\" represents a literal backslash.
Arevos is offline   Reply With Quote
Old Dec 12th, 2006, 6:11 PM   #3
Xian_Fung
Newbie
 
Join Date: Jun 2006
Posts: 15
Rep Power: 0 Xian_Fung is an unknown quantity at this point
-_-

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
Xian_Fung is offline   Reply With Quote
Old Dec 12th, 2006, 6:27 PM   #4
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
As I said, "\\" represents a "\". So in order to replace "\" with "/", you'll need code that looks like this:
dir.replace("\\", "/");
Arevos is offline   Reply With Quote
Old Dec 12th, 2006, 7:21 PM   #5
Xian_Fung
Newbie
 
Join Date: Jun 2006
Posts: 15
Rep Power: 0 Xian_Fung is an unknown quantity at this point
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.
Xian_Fung is offline   Reply With Quote
Old Dec 12th, 2006, 7:47 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Dec 12th, 2006, 8:09 PM   #7
Xian_Fung
Newbie
 
Join Date: Jun 2006
Posts: 15
Rep Power: 0 Xian_Fung is an unknown quantity at this point
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
Xian_Fung is offline   Reply With Quote
Old Dec 12th, 2006, 8:20 PM   #8
AntiNinja
Hobbyist Programmer
 
AntiNinja's Avatar
 
Join Date: Jun 2006
Location: The States
Posts: 101
Rep Power: 3 AntiNinja is on a distinguished road
Send a message via AIM to AntiNinja Send a message via Yahoo to AntiNinja
Did you even try Dawei's solution?
Quote:
In literal Java strings the backslash is an escape character. The literal string "\\" is a single backslash. In regular expressions, the backslash is also an escape character. The regular expression \\ matches a single backslash. This regular expression as a Java string, becomes "\\\\". That's right: 4 backslashes to match a single one.
__________________
Pain is just weakness leaving the body.
AntiNinja is offline   Reply With Quote
Old Dec 12th, 2006, 8:35 PM   #9
Xian_Fung
Newbie
 
Join Date: Jun 2006
Posts: 15
Rep Power: 0 Xian_Fung is an unknown quantity at this point
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.
Xian_Fung is offline   Reply With Quote
Old Dec 12th, 2006, 8:46 PM   #10
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
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('\\', '/');
You may need to assign the value returned by the replace function to the original variable, as the function does not modify the original String.

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("\\\\", "/");
Hope that helps.
titaniumdecoy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
solved - Error before token "{" Daniel_kd C 3 Apr 15th, 2005 12:00 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:41 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC