• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

problems with replaceAll and slash character

 
Ranch Hand
Posts: 49
  • 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?
 
Marshal
Posts: 70224
282
  • 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
  • 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
Marshal
Posts: 70224
282
  • 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
  • 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
Marshal
Posts: 70224
282
  • 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
  • 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
Marshal
Posts: 70224
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

jean-gobert de coster wrote:. . .) thanks for the help anyways ;-)

 
I'm doing laundry! Look how clean this tiny ad is:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic