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
    Posts
    43
    Rep Power
    0

    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ž");
                System.out.println(testVars[k]);
            }
        }
                
    }
    The output I am getting is:

    ʤUser1
    ʤUser2
    ʤUser3


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

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,381
    Blog Entries
    7
    Rep Power
    20

    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,

    Jos
    quad64bit likes this.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

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