• 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

replaceAll does not work ?

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


al.get(i).toString() returns forinstance: "href='?mode=fam&fam=Micropterigidae'"
then obviouisly
al.get(i).toString().replaceAll("'", "\"") returns: "href="?mode=fam&fam=Micropterigidae""

Nothing gets replaced. ?
 
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you look closely, those two strings are different. It has made exactly the replacement you requested it to. All of the single quotes in the original have been replaced with double quotes. What replacement where you expecting?

Note that the backlash character is a special character in Java String literals. It is the escape character. You place it before any special character to make Java put that character in the String.

Normally the double quote character starts or ends a String literal. "\"" is a String literal containing a single double quote character.
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's quite an odd usage of .replaceAll() where you're being very specific about what text you are searching for within responseBody. I assume that responseBody String does actually contain the text "href='?mode=fam&fam=Micropterigidae'"?
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Mike. The replaceAll() seems to be doing what I'd expect, i.e., returning a string with single quotes replaced by double quotes. Maybe Mike's right; you just didn't notice the difference? Or maybe the problem is that you're expecting it to modify the original string? None of the String methods do that, because String is an immutable class. The methods just return a brand new String object with the modifications you requested, and it's up to you work with the new object instead of the old.

Another odd thing is that you're calling replaceAll() twice in your quoted code, one call nested in the other. Is that your intention?
 
Miran Cvenkel
Ranch Hand
Posts: 245
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No it is not working because it can't. Figured why.

if I use forinstance this string (in form of string variable in upper code) as regex
"?mode=fam&fam=Micropterigidae"
it reads ? as special regex char, hence replacement does not work.

As I read, using replace replaces all, same as replaceAll, without using regex.
I guess that should work.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah yes, good spot. You will also need to escape all of the characters that are special to the regular expression engine.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean use Pattern.quote for that, and Matcher.quoteReplacement for the replacement?
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Miran Cvenkel. Well done on figuring out the problem you were having, and kudos for coming back and telling us about it. Have a cow. Moo!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic