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 '/' ???
> 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
> 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
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'.
--
jmm dash list (at) sohnen-moe (dot) com
(Remove .AXSPAMGN for email)
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?
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)
so having RTFJD before I'd posted :-) and consulting them again now, I still
don't see why this should throw an exception.
--
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 :-)
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("\\","/")
"Hmm" what? replaceAll() is not replace(), doesn't