• 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 not working

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to replace "&" with "&" but it is not working. Here is the code.

import java.io.*;
import java.util.*;

public class searchReplaceMethod
{
public static void main ( String args[])

{
String tobsearched = "AT&T Bell Laboratories";

tobsearched.replaceAll("&","&");

System.out.println(tobsearched);

}
}

Please help.
Thanks,
Nee
[ March 07, 2005: Message edited by: Jim Yingst ]
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm. Replacing "&" with "&" - that's not going to visably change the String. Are you sure you want to do this?
 
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Am I missing something?

How are you determining that it doesn't work?
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First off, like everyone else is asking, "How do you know it's not working?". But more importantly this line is not going to do what you want


You should be assigning it to a new String like so.

then print out the new String.

Of course that won't look like it works either if you insist on replacing "&" with "&". Instead try replacing "&" with "@"!
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming you were replacing & with something you could recognize as changed...

You are not saving the changed String that replaceAll returns. Instead you are printing out the original String.

Try this:

 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have edited the original post in this thread to reflect what Nee Kat meant to say, instead of what everyone's browser displayed. The basic problem is: the original post was intended to say

I am trying to replace "&" with "&"

but when you type & in a post here, the browser will render it as simply &. So the original post came out as

I am trying to replace "&" with "&"

which is why all the subsequent posters were confused. If you want to get & to display here, you need to escape the original & with an entity - which means you need to type "&" instead of "&". It's confusing, I know - but necessary. Remember that you can edit your own posts after you post them, using the little pencil-and-paper icon. So if you get it wrong, you can always fix it later.

I have edited the original post to make it more clear, but I haven't touched the subsequent posts, since I wasn't sure how to make them consistent with the new version of the first post. But everyone should be aware that the previous posts were responding to a different version of the original post - that's why they don't seem to make sense now.

Hentay and Carol have identified the real problem though - replaceAll() doesn't change the original string, it creates a new one. So you need to save that new string in a variable.
 
Nee Kat
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,

I got it.

Nee
 
reply
    Bookmark Topic Watch Topic
  • New Topic