my dog learned polymorphism
The moose likes Beginning Java and the fly likes replaceAll not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "replaceAll not working" Watch "replaceAll not working" New topic

replaceAll not working

Nee Kat
Ranch Hand

Joined: Jan 27, 2004
Posts: 37
I am trying to replace "&" with "&" but it is not working. Here is the code.

import java.util.*;

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

String tobsearched = "AT&T Bell Laboratories";




Please help.
[ March 07, 2005: Message edited by: Jim Yingst ]
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Hmm. Replacing "&" with "&" - that's not going to visably change the String. Are you sure you want to do this?

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
Am I missing something?

How are you determining that it doesn't work?
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
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 "@"!
Carol Enderlin
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
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:

Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
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.

"I'm not back." - Bill Harding, Twister
Nee Kat
Ranch Hand

Joined: Jan 27, 2004
Posts: 37

I got it.

subject: replaceAll not working
It's not a secret anymore!