• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

replacing string parts

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone!

I'm trying to replace "/" to "\" in a string. I'm using this:

However, i keep getting StringIndexOutOfBoundsException. Why is that?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The second argument is a regular expression, meaning that that not only is the Java compiler going to look at it (necessitating that double backslash) but the regular expression compiler is going to look at it too -- and as it turns out, "\" is the escape character in regular expressions as well. As a result you actually have to use four backslashes to get a single one into a regular expression from Java source!

path = path.replaceAll( "/", "\\\\" );
 
Csaba Kassitzky
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, i would never thought of that!
Thanks!
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're using JDK 5, you can also use

The difference between replace() and replaceAll() is not obvious from the name - actually replace() replaces all occurrances of whatever you're replacing, much like replaceAll(). The real difference is that replace() is simple and does exacly what you'd expect it to do, while replaceAll() has more complex behavior.

Warning - the following is a bit outside the scope of the "Beginner" forum, so some of you may just want to skip this post. But it does follow directly from the preceding conversation.

[EFH]: The second argument is a regular expression,

The first argument is a regular expression (though that doesn't happen to create any problems in this particular case since a / is not a special character for Java regular expressions). The second argument is not a regular expression, but a replacement string - which has its own rules, much simpler than a regular expression, but hard to find in the documentation. The clearest explanation to be found in the API (after following several intermediate links) is in the Matcher class, under the appendReplaceMent() method. Basically, in a replacement string, \ and $ characters have special significance, so if you want a "plain" \ or $, you have to insert an extra \ in front. And in a Java literal each \ must be doubled to \\. As previously noted.

In JDK 5 they added the replace() method show above, which is much simpler to use. Also there are two static methods which can be used to escape any special characters in a given expression, so that you get a string that "means" the same thing as the original literal string would have (if not for special characters).

Pattern.quote(str) - use this for a value that can be used as a regular expression

Matcher.quoteReplacement(str) - use this for a value that can be used as a replacement string

In other words, if str, x, and y are Strings, then

is equivalent to

Obviously the first form is easier to read - the second form just shows who the methods are related.
[ December 06, 2005: Message edited by: Jim Yingst ]
 
Csaba Kassitzky
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What should i write in if i want to do the opposite replacement?
replaceAll("\\", "/");?
or should i just use replace("\\", "/"); as you mentioned?
I'm using Java 1.5 btw.
 
Csaba Kassitzky
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I encountered another replacing problem:
Got a String caled str. It contains element.
Element is ".\0_1.txt" right now.
I have this line in my prog:

in this case id is 0 and counter is 1, so i'm replacing element
with: ".\0_1.txt" (spec case, but still should work)
However, i get this message:

What's wrong with this?
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The replaceFirst() method treats its arguments much the same as replaceAll() did. So most of the previous discussion still holds - except that there is no "simplified" version of replaceFirst(), the way replace() was a simplified version of replaceAll().

From what you say, the code you're running is equivalent to

You have correctly escaped the \ in the second argument - but not the first. This may seem more complex because the first argument is in a variable. But you can simply use
 
Csaba Kassitzky
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, it works fine now!
 
Do you pee on your compost? Does this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic