Re: Convert \ to /

From: PerfectDayToChaseTornados (pdtct_at_emailaddress.invalid)
Date: 12/02/04


Date: Thu, 02 Dec 2004 20:23:01 GMT


"Eric Sosman" <eric.sosman@sun.com> wrote in message
news:conse7$fb2$1@news1brm.Central.Sun.COM...
| 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
| > suggestions from this group and none work. Is there a straight forward
| > way in Java to replace '\' with '/' ???
|
| 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.Sosman@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)


Relevant Pages

  • Re: gfortran diagnostics and so on
    ... Well, in f0003, backslash is part of the standard Fortran character set. ... Because the backslash is part of the standard Fortran character set, the default behavior should be the printable character, **NOT** some kind of magic introductory character that transforms the interpretation of following character. ... The one I like best is to use one of the popular extensions to designate a particular literal string according to the C language. ...
    (comp.lang.fortran)
  • Re: Unrecognized escape sequences in string literals
    ... character without knowing the context, ... '\n' maps to the string chr. ... In both cases the backslash in the literal have the same meaning: ... escape every backslash, ...
    (comp.lang.python)
  • Re: Is CL good at Text Processing?
    ... Each string contains a list of titles. ... It's Clojure, not Common Lisp, and uses a few Java string-handling methods. ... It AVOIDS using the regexp features of Clojure, since I think that makes a fairer test of how easy it is to do this stuff in Lisp in general. ... String and Character methods. ...
    (comp.lang.lisp)
  • Re: Unrecognized escape sequences in string literals
    ... how unrecognized escape sequences are treated in Python. ... a backslash is just an ordinary character, ... Why should a backslash in a string literal be an error? ...
    (comp.lang.python)
  • Re: how do I expand a unicode string to its visual UTF8 representation?
    ... What Java uses internally to represent String ... Java was conceived with Unicode 3.0 in mind, ... which does *not* represent a character ...
    (comp.lang.java.programmer)