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

Thread: String replace not working properly

  1. #1
    phyzix5761 is offline Member
    Join Date
    Nov 2012
    Rep Power

    Default String replace not working properly

    This is a small example of my code where I am trying to replace a char with another while iterating through a for loop. The problem is that it's not replacing the char.

    Java Code:
    public class Test
        private static String[] testVars = {"ʤUser1", "ʤUser2", "ʤUser3"};
        public static void main(String[] args)
            for (int k = 0; k < testVars.length; k++)
                testVars[k].replace("ʤ", "Dž");
    The output I am getting is:


    EDIT Found the solution. Needed to create a string to hold the replacement.
    Last edited by phyzix5761; 11-23-2012 at 07:51 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default Re: String replace not working properly

    Strings are immutable so you have to assign the return value of the replace method to the reference variable again; i.e.

    Java Code:
    testVars[k]= testVars[k].replace("ʤ", "Dž");
    kind regards,

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

Similar Threads

  1. For loop not working properly
    By phyzix5761 in forum New To Java
    Replies: 5
    Last Post: 11-18-2012, 08:59 PM
  2. setCaretPosition not working properly
    By phyzix5761 in forum AWT / Swing
    Replies: 6
    Last Post: 11-17-2012, 10:11 AM
  3. NetBeans IDE not working properly
    By farmer in forum NetBeans
    Replies: 2
    Last Post: 12-09-2011, 04:12 PM
  4. Why this block is not working properly?
    By juhiswt in forum New To Java
    Replies: 2
    Last Post: 03-13-2011, 12:08 PM
  5. Log4j not working properly....
    By prakash_dev in forum Advanced Java
    Replies: 0
    Last Post: 03-17-2008, 01:13 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