This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Issue with String replacement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

Issue with String replacement

Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Posts: 93




Where as the output i was expecting was:


What am i doing wrong here??

-Adi

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Can you provide an SSCCE please?
Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Posts: 93

Jeff Verdegan: does that make it better?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Aditya Sirohi wrote:Jeff Verdegan: does that make it better?


Did you click the link and read the page? I'm looking for something I can copy, paste, compile, and run, and get the same results as you.
Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Posts: 93

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18505
    
  40

Aditya Sirohi wrote:



Without actually testing it, my first instinct says that the issue is at line 10 -- specifically the replacement string that was created at line 8. The second parameter to the replaceAll() method is not a regular java string -- it is a regex replacement string. With a regex replacement string, the backslash has special meaning, so if you want to replace with a literal backslash you need to escape the backslash.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Posts: 93

Thanks, aah my eyes.. This is what happens when you work 2nights straight back on back

This change worked:- String replacedVal = remoteDirMSI.replaceAll("/", "\\\\\\\\");

Regards
Adi
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18505
    
  40

Aditya Sirohi wrote:Thanks, aah my eyes.. This is what happens when you work 2nights straight back on back

This change worked:- String replacedVal = remoteDirMSI.replaceAll("/", "\\\\\\\\");



Of course, the next question is... do you know why it is eight backslashes???

Anyway, it is probably easier if you use the built-in method to disable the regex replacement string features. To do this, change this...



to this...



Henry
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1502
    
    5

Henry Wong wrote:do you know why it is eight backslashes???

I didn't get this. Can you provide a short explanation (or links to it)?
Thanks.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Isn't this the same problem as http://www.coderanch.com/t/568814/java/java/Find-replace-pattern-String? You were already told to not use replaceAll but replace instead.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37913
    
  22
Rob Spoor wrote:Isn't this the same problem as . . .
Thank you for noticing, Rob. If this is a duplicate thread, then I shall close it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Issue with String replacement
 
Similar Threads
Development
Pass By Value Problem
File Class
Find and replace a pattern in a String
cast to bytes