[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Question about using String.replace()

Posted on 2004-11-23
13
Medium Priority
?
251 Views
Last Modified: 2010-03-31
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.
0
Comment
Question by:chenwei
  • 9
  • 3
13 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 12655135
> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 12655146
You should have
test2 = test2.replace(test2.charAt(4), ':');
0
 
LVL 35

Expert Comment

by:girionis
ID: 12655148
> 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

by:zzynx
ID: 12655157
Maybe better use:

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

;°)
0
 
LVL 35

Expert Comment

by:girionis
ID: 12655188
> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 12655257
>> 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
 
LVL 37

Expert Comment

by:zzynx
ID: 12655276
>> 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
 

Author Comment

by:chenwei
ID: 12655460
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12655496
Use a StringBuffer and the function setCharAt()
0
 
LVL 37

Accepted Solution

by:
zzynx earned 80 total points
ID: 12655518
String test = "this is a test";
StringBuffer buf = new StringBuffer(test);
buf.setCharAt(4, ':');
System.out.println(buf.toString());
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12655528
test = buf.toString();
if you want the original string to be changed
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12655535
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
 
LVL 37

Expert Comment

by:zzynx
ID: 12655836
Thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month19 days, 14 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question