Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Convert \ to /

1 view
Skip to first unread message

K McNeil

unread,
Dec 2, 2004, 2:36:24 PM12/2/04
to
Why is it so hard to convert backslashes to forward slashes in java?
My input form gets the file as C:\temp\file.pdf and we all know \ is
the escape character.

I tried
String d = C:\temp\file.pdf;
d.replace('\\', '/') and it doesn't work, I also tried the 100 other
suggestions from this group and none work. Is there a straight forward
way in Java to replace '\' with '/' ???

Message has been deleted

Andrew Thompson

unread,
Dec 2, 2004, 3:33:12 PM12/2/04
to
On Thu, 02 Dec 2004 20:23:01 GMT, PerfectDayToChaseTornados wrote:

> String e = d.replaceAll("\\","/");

String e = d.replace('\\', '\');

And please locate your delete key[1] PDTCT, that post did not need
to requote all 73 lines of earlier response.

[1] <http://www.physci.org/kbd.jsp?key=del>

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.LensEscapes.com/ Images that escape the mundane

Andrew Thompson

unread,
Dec 2, 2004, 3:09:55 PM12/2/04
to
On 2 Dec 2004 11:36:24 -0800, K McNeil wrote:

> String d = C:\temp\file.pdf;
> d.replace('\\', '/')

You might try posting code that actually compiles in future, rather
than waste our time woth code that is 'something like' what you are
trying.

<sscce>
public class TestStringReplace {
public static void main(String[] args) {


String d = "C:\\temp\\file.pdf";

// Even if your example compiled, you are throwing
// the result away!
d = d.replace('\\', '/');
System.out.println(d);
}
}
</sscce>

HTH

Eric Sosman

unread,
Dec 2, 2004, 5:38:31 PM12/2/04
to
PerfectDayToChaseTornados wrote:
> | "Hmm" what? replaceAll() is not replace(), doesn't
> | perform the same operation, and doesn't assign the same
> | meanings to its arguments. RTFJD.
> |
> | --
> | Eric....@sun.com
> |
> Certainly doesn't, but as McNeil was having trouble I tried to help. I
> usually use replaceAll() for this kind of operation (being a big fan of
> regular expressions) & thought that this should work. Apologies if I
> confused you :-)
> from the docs
> 'replaceAll(String regex, String replacement)
> Replaces each substring of this string that matches the given
> regular expression with the given replacement.'
> and looking at the Javadoc for Pattern, "\\" would seem to be a valid
> regular expression
> again from the docs
> \\ The backslash character
>
> so having RTFJD before I'd posted :-) and consulting them again now, I still
> don't see why this should throw an exception.

Aha! You're confused by the interaction of two
escape mechanisms that happen to have some superficial
similarities.

Yes, the regular expression consisting of two
backslash characters is valid, and it matches a target
sequence consisting of one backslash. You need two in
the regex because the backslash has a special meaning
to the Pattern compiler, and you need to escape that
meaning. The first backslash says "something special
is coming," and the second says "the special thing is
a backslash stripped of its special meaning."

But when you write "\\" in a Java source file, how
long is the resulting String? It's *one* character long
and contains *one* backslash, not two, because Java also
gives backslash a special meaning inside literals. The
two source backslashes become one "delivered" backslash
character.

So when you use the one-character String "\\" as a
regular expression, the Pattern compiler sees the backslash
and says "something's afoot; the next character will tell me
about it." Then it finds that the next character doesn't
exist; the introductory backslash was all alone -- and it
throws an exception to tell you about the malformed regex.

To sum up, the regex that matches a single backslash
character consists of two successive backslashes. When you
write a String literal in Java source, you need two "source"
backslashes for each "delivered" backslash. So what you
should have written is d.replaceAll("\\\\", "/") -- the
first two source backslashes become the first backslash in
the regex, and the next two become the second, and the two
backslashes in the regex match a single backslash in the
scanned string `d'.

--
Eric....@sun.com


jmm-list-gn

unread,
Dec 2, 2004, 5:24:21 PM12/2/04
to
K McNeil wrote:
> Why is it so hard to convert backslashes to forward slashes in java?
> My input form gets the file as C:\temp\file.pdf and we all know \ is
> the escape character.
>
> I tried
> String d = C:\temp\file.pdf;
>
If the string is actually as you show it (presumably with quotes), the
characters are already escaped so there is no backslash to replace. The
string must already have "\\".
Even if you are reading from a file (or pipe, or ...), the single
backslash is interpreted as the escape character during input. One way to
handle this is to preprocess the text external to Java to convert "\" to
either "\\" or "/". Another way is to open the file as binary; then you
could scan the input one byte at a time.

--
jmm dash list (at) sohnen-moe (dot) com
(Remove .AXSPAMGN for email)

Eric Sosman

unread,
Dec 2, 2004, 3:03:18 PM12/2/04
to

It's too bad you didn't post your actual code,
because there are three different things I can think
of that you may be doing wrong, and I don't know which
to explain. You might even have come up with a fourth
type of error I haven't thought of, in which case all
the explanation below will do you no good at all ...
In the future, please provide a short, complete, and
compilable example that demonstrates whatever problem
you may be having; otherwise, those who answer you are
forced to guess and may guess incorrectly.

So.

Possible Problem Number One: The replace() method
does not alter its String, but instead creates and
returns a brand-new String with all the replacements
carried out. If you wrote

d.replace(...);
System.out.println(d);

you would see no change, because the String has not in
fact been changed. Instead, write

String e = d.replace(...);
System.out.println(e);

(There are, of course, other ways to write this.)

Possible Problem Number Two: If your actual code was

String d = "C:\temp\file.pdf";

(the quotes were missing from the code you supplied), then
it would compile just fine but the String would not contain
the characters you might have expected. In particular, it
would not contain any backslash characters at all! The
character immediately after the colon would be the tab
control character, and the one right after "emp" would be
the form-feed control character. If you actually want to
get a backslash character into a string literal in Java
source code, you must escape it:

String d = "C:\\temp\\file.pdf";

Possible Problem Number Three: The escape mechanism
mentioned above applies only to literals in Java source
files, not to actual sequences of characters that come,
say, from a Reader or an InputStream. If somebody keys
"Cee, colon, backslash, tee, ee, ..." into a JTextField,
there is nothing special about the backslash: it is a
plain, ordinary character in its own right, doesn't combine
with the subsequent "t", and doesn't need to be escaped.
It may be that all the work you're trying to do is in fact
completely unnecessary.

Good luck -- and next time, post real code, okay?

--
Eric....@sun.com


PerfectDayToChaseTornados

unread,
Dec 2, 2004, 3:23:01 PM12/2/04
to

"Eric Sosman" <eric....@sun.com> wrote in message
news:conse7$fb2$1...@news1brm.Central.Sun.COM...

Hi Eric, this does seem to be a tricky one

public static void main(String[] args) {

String d = "C:\\temp\\file.pdf";

String e = d.replaceAll("\\","/");

System.out.println("e = " + e);
}

java.util.regex.PatternSyntaxException: Unexpected internal error near index
1
\
^
at java.util.regex.Pattern.error(Pattern.java:1528)
at java.util.regex.Pattern.compile(Pattern.java:1286)
at java.util.regex.Pattern.<init>(Pattern.java:1035)
at java.util.regex.Pattern.compile(Pattern.java:779)
at java.lang.String.replaceAll(String.java:1663)

hmm :-)
--
-P
"Programs that are hard to read are hard to modify.
Programs that have duplicated logic are hard to modify.
Programs with complex conditional logic are hard to modify"

( Kent Beck)


PerfectDayToChaseTornados

unread,
Dec 2, 2004, 5:18:18 PM12/2/04
to

| "Hmm" what? replaceAll() is not replace(), doesn't
| perform the same operation, and doesn't assign the same
| meanings to its arguments. RTFJD.
|
| --
| Eric....@sun.com
|
Certainly doesn't, but as McNeil was having trouble I tried to help. I
usually use replaceAll() for this kind of operation (being a big fan of
regular expressions) & thought that this should work. Apologies if I
confused you :-)
from the docs
'replaceAll(String regex, String replacement)
Replaces each substring of this string that matches the given
regular expression with the given replacement.'
and looking at the Javadoc for Pattern, "\\" would seem to be a valid
regular expression
again from the docs
\\ The backslash character

so having RTFJD before I'd posted :-) and consulting them again now, I still
don't see why this should throw an exception.

--

PerfectDayToChaseTornados

unread,
Dec 2, 2004, 6:43:49 PM12/2/04
to

Thanks Eric, realized my mistake, about 2 mins after posting this :-) Did
cancel the post & re post, but my ISPs a bit slow propagating at the moment!

Many thanks for your reply any way :-)

PerfectDayToChaseTornados

unread,
Dec 2, 2004, 6:11:43 PM12/2/04
to

"Eric Sosman" <eric....@sun.com> wrote in message
news:coo03d$h22$1...@news1brm.Central.Sun.COM...
| PerfectDayToChaseTornados wrote:

| > "Eric Sosman" <eric....@sun.com> wrote:
|
|
| "Hmm" what? replaceAll() is not replace(), doesn't
| perform the same operation, and doesn't assign the same
| meanings to its arguments. RTFJD.
|
| --
| Eric....@sun.com

Oops, sorry had RTFJD :-), but as I'm in the habit of using replaceAll(), (I
like regular expressions) I had not read post properly & assumed (wrongly)
he was using replaceAll(). Mine threw the error because of needing to escape
the escape & escape that again :-)

Should of course have been d.replaceAll("\\\\","/") not
d.replaceAll("\\","/")

Eric Sosman

unread,
Dec 2, 2004, 4:05:48 PM12/2/04
to
PerfectDayToChaseTornados wrote:
> "Eric Sosman" <eric....@sun.com> wrote:
> | K McNeil wrote:
> | > Why is it so hard to convert backslashes to forward slashes in java?
> | > My input form gets the file as C:\temp\file.pdf and we all know \ is
> | > the escape character.
> | >
> | > I tried
> | > String d = C:\temp\file.pdf;
> | > d.replace('\\', '/') and it doesn't work, I also tried the 100 other
> | > [...]

>
> Hi Eric, this does seem to be a tricky one
>
> public static void main(String[] args) {
> String d = "C:\\temp\\file.pdf";
> String e = d.replaceAll("\\","/");
> System.out.println("e = " + e);
> }
>
> java.util.regex.PatternSyntaxException: Unexpected internal error near index
> 1
> \
> ^
> at java.util.regex.Pattern.error(Pattern.java:1528)
> at java.util.regex.Pattern.compile(Pattern.java:1286)
> at java.util.regex.Pattern.<init>(Pattern.java:1035)
> at java.util.regex.Pattern.compile(Pattern.java:779)
> at java.lang.String.replaceAll(String.java:1663)
>
> hmm :-)

"Hmm" what? replaceAll() is not replace(), doesn't

0 new messages