aspose file tools*
The moose likes Java in General and the fly likes problems with replaceAll and slash character Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "problems with replaceAll and slash character" Watch "problems with replaceAll and slash character" New topic
Author

problems with replaceAll and slash character

jean-gobert de coster
Ranch Hand

Joined: Dec 04, 2008
Posts: 49
Hi,
I have a string

String myString="/folder 2";

and I'm trying to replace the / with a \

myString= myString.replaceAll("/","\\\\");

but in the end, nothing is replaced and myString is still "/folder 2"

I really don't understand why this isn't working, anyone has a clue?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38873
    
  23
[Campbell@localhost java]$ javac -d . SlashReplacement.java
[Campbell@localhost java]$ java trivia.SlashReplacement /myLocation/myFile / \\\\
\myLocation\myFile
jean-gobert de coster
Ranch Hand

Joined: Dec 04, 2008
Posts: 49
ok, so it seems to be working fine with you :-( which is good news because that's how it's supposed to work...

I was doing some more treatments to the string before, so that may be the problem.

My original code was this:



I changed it to



and now it works... go figure
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38873
    
  23
That is different from what you posted earlier. Do you really want the .*? after / ?
You realise the .* can match zero occurrences of anything (except line end), so that might cause problems?

Try breaking those chained calls into individual statements and insert System.out.println calls between successive lines, so you can see what is happening.
jean-gobert de coster
Ranch Hand

Joined: Dec 04, 2008
Posts: 49
from the getPath method I'm calling before decodint, I get a String that looks like this

/app:company_home/cm:folder_x0020_1/cm:_x0030_054321.pdf

what i need is to turn this into

\folder 1\0054321.pdf

so yes I need to match /.*?:

My example was simplified but I based myself of what I know from playing around with the log outputs. If I simply remove the .replaceAll("/","\\\\") the log gives this as output:

/app:company_home/cm:folder 1/cm:0054321.pdf
/folder 1/0054321.pdf

adding the .replaceAll("/","\\\\") and the log gives this output

/app:company_home/cm:folder 1/cm:0054321.pdf
/folder 1/0054321.pdf

but nevermind this, since the second code example makes those things work just right, and is actually better in all aspects.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38873
    
  23
Maybe a regular expression to match those hex numbers? I am not used to seeing x0020, but usually see %20 in URLs.

But if it is working . . . leave well alone
jean-gobert de coster
Ranch Hand

Joined: Dec 04, 2008
Posts: 49
Campbell Ritchie wrote:Maybe a regular expression to match those hex numbers? I am not used to seeing x0020, but usually see %20 in URLs.

But if it is working . . . leave well alone


the _x0020_ are XPath encoding, but the ISO9075.decode() method makes sure they are translated

Yeah I'll leave it thanks for the help anyways ;-)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38873
    
  23
jean-gobert de coster wrote:. . .) thanks for the help anyways ;-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problems with replaceAll and slash character