Re: Convert \ to /
From: PerfectDayToChaseTornados (pdtct_at_emailaddress.invalid)
Date: 12/02/04
- Next message: Eric Sosman: "Re: Boolean values with no start value?"
- Previous message: Andrew Thompson: "Re: Convert \ to /"
- In reply to: Eric Sosman: "Re: Convert \ to /"
- Next in thread: Andrew Thompson: "Re: Convert \ to /"
- Reply: Andrew Thompson: "Re: Convert \ to /"
- Reply: Eric Sosman: "Re: Convert \ to /"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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)
- Next message: Eric Sosman: "Re: Boolean values with no start value?"
- Previous message: Andrew Thompson: "Re: Convert \ to /"
- In reply to: Eric Sosman: "Re: Convert \ to /"
- Next in thread: Andrew Thompson: "Re: Convert \ to /"
- Reply: Andrew Thompson: "Re: Convert \ to /"
- Reply: Eric Sosman: "Re: Convert \ to /"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|