Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By JosAH

Thread: Using the replace method twice on the same String?

  1. #1
    dman255 is offline Member
    Join Date
    Nov 2015
    Posts
    1
    Rep Power
    0

    Default Using the replace method twice on the same String?

    Hello everyone! I am rather new to Java and I have a question about using the replace method for two different letters on the same String. I will copy paste the code I have written so far below.

    public class ReplaceTester {
    public static void main(String[] args) {
    String river = "Mississippi";
    String replaceI = river.replace("i", "!");
    String replaceS = river.replace("s", "$");
    String replaceBoth = replaceS + replaceI;
    System.out.println(replaceBoth);
    }

    }

    Here is what is printed in the Output box:

    Mi$$i$$ippiM!ss!ss!pp!

    My goal is to replace all of the s's with $. And replace all of the i's with !. Except I need it all in one word instead of splitting it into two like it is now. If anyone can shed some light on how I could achieve this it would be much appreciated! Thanks in advance!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Using the replace method twice on the same String?

    Look at what the code prints. Track the contents of the String that is printed back to its source.
    The code has created 2 different Strings with different replacements.
    If you want only one String then do the replacements on only one String serially, not on two Strings in parallel.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Using the replace method twice on the same String?

    One key to remember with Strings...a String object is immutable, that is it is not possible to change its contents.
    So the methods on String that "change" a String actually only return a new String, so the original is untouched.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Using the replace method twice on the same String?

    The same behaviour shows up with simple numbers: e.g. f(x) := x+1 and g(x) := 2*x. applying f(g(x)) := 2*x+1 and g(f(x)) := 2*(x+1), while doing something like this: y= f(x), z= g(x), z+y yields just not what you had in mind. Back to your example: imagine that f(x) replaces all 'i's in String x and g(x) replaces all 's's in String x; if you want to replace both of them, you have to apply f(g(x)) or g(f(x)), in your example it becomes:

    Java Code:
    String result= river.replace("i", "!").replace("s", "$");
    kind regards,

    Jos
    RamyaSivakanth likes this.
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. ---HELP (replace method)---
    By Zitro in forum New To Java
    Replies: 3
    Last Post: 02-04-2012, 12:53 AM
  2. String.replace ?!
    By HearT.Hunt3r in forum New To Java
    Replies: 4
    Last Post: 08-22-2011, 03:04 AM
  3. Replace String
    By Raeghin in forum New To Java
    Replies: 1
    Last Post: 07-28-2009, 03:58 PM
  4. Using a replace method...
    By paul in forum New To Java
    Replies: 2
    Last Post: 08-07-2007, 04:50 AM
  5. String replace method
    By venkata.tarigopula in forum Advanced Java
    Replies: 1
    Last Post: 07-10-2007, 08:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •