Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problems with replaceAll and slash character

 
jean-gobert de coster
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47286
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Campbell@localhost java]$ javac -d . SlashReplacement.java
[Campbell@localhost java]$ java trivia.SlashReplacement /myLocation/myFile / \\\\
\myLocation\myFile
 
jean-gobert de coster
Ranch Hand
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47286
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47286
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47286
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jean-gobert de coster wrote:. . .) thanks for the help anyways ;-)
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic