Question about using String.replace()

my runnable program lokks as follow:

public class MyTest {

  private static String test1 = "this is a test";
  private static String test2 = "this is another test";
      
  public static void main(String[] args) {
   System.out.println(test1.replace(test1.charAt(4), ':'));   // 1. centence
            
   test2.replace(test2.charAt(4), ':');      // 2. centence
   System.out.println(test2);      // 3. centence
  }
}

The result are:
this:is:a:test
this is another test

I wonder why the 2.centence doesn't replace the char.
chenweiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

girionisCommented:
> I wonder why the 2.centence doesn't replace the char.

Because you do not assign the new "test2" value somewhere and so "test2" still holds the old value (without the spaces).

0
zzynxSoftware engineerCommented:
You should have
test2 = test2.replace(test2.charAt(4), ':');
0
girionisCommented:
> System.out.println(test1.replace(test1.charAt(4), ':'));   // 1. centence

You will also see no ":" if you replace the line above with:

test1.replace(test1.charAt(4), ':')
System.out.println(test1);
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

zzynxSoftware engineerCommented:
Maybe better use:

test2.replaceAll(" ", ":");
System.out.println(test2);

;°)
0
girionisCommented:
> test2.replace(test2.charAt(4), ':');      // 2. centence

Here you just replace all " " characters with ":" characters but you do not assign the result somewhere else. You will need another place to store the newly created string (the one that has : instead of spaces) otherwise you cannot see the changes since the first string still holds the old value.
0
zzynxSoftware engineerCommented:
>> Here you just replace all " " characters with ":" characters but you do not assign the result somewhere else.
That's why you better use replaceAll(). It automatically assigns the resulting String to the original
0
zzynxSoftware engineerCommented:
>> It automatically assigns the resulting String to the original
In fact, replaceAll() just performs the replace action onto the original string
while replace() just returns the adapted string *without touching the original*
0
chenweiAuthor Commented:
thanks for the info.

is there any function which just replace the char at one position? such as:

String test = "this is a test";
...
...
  test.function(test.chatAtPos(5), ':');
...
...
the result should be:
 "this:is a test".

0
zzynxSoftware engineerCommented:
Use a StringBuffer and the function setCharAt()
0
zzynxSoftware engineerCommented:
String test = "this is a test";
StringBuffer buf = new StringBuffer(test);
buf.setCharAt(4, ':');
System.out.println(buf.toString());
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zzynxSoftware engineerCommented:
test = buf.toString();
if you want the original string to be changed
0
zzynxSoftware engineerCommented:
If you wonder why:

Strings are constant; their values cannot be changed after they are created.
String buffers support mutable strings.
A string buffer implements a mutable sequence of characters.
0
zzynxSoftware engineerCommented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.