Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   "\" (http://www.programmingforums.org/showthread.php?t=12174)

Xian_Fung Dec 12th, 2006 5:24 PM

"\"
 
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.

Arevos Dec 12th, 2006 5:42 PM

Wow. Deja Vu. In almost all programming languages, the string "\\" represents a literal backslash.

Xian_Fung Dec 12th, 2006 6:11 PM

-_-
 
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?

Arevos Dec 12th, 2006 6:27 PM

As I said, "\\" represents a "\". So in order to replace "\" with "/", you'll need code that looks like this:
:

dir.replace("\\", "/");

Xian_Fung Dec 12th, 2006 7:21 PM

Ok. One problem with your solution though, I tried that earlier and it didn't work.

DaWei Dec 12th, 2006 7:47 PM

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 "\\\\".

Xian_Fung Dec 12th, 2006 8:09 PM

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.

AntiNinja Dec 12th, 2006 8:20 PM

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.

Xian_Fung Dec 12th, 2006 8:35 PM

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 "\\".

titaniumdecoy Dec 12th, 2006 8:46 PM

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.


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