Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

replaceAll not working

 
Nee Kat
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • 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 ]
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • 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?
 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I missing something?

How are you determining that it doesn't work?
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • 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 "@"!
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • 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:

 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks,

I got it.

Nee
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic