Results 1 to 2 of 2
- 11-23-2012, 06:46 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 43
- Rep Power
- 0
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.
The output I am getting is: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]); } } }
ʤ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.
- 11-23-2012, 06:52 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
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.
kind regards,Java Code:testVars[k]= testVars[k].replace("ʤ", "Dž");
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
For loop not working properly
By phyzix5761 in forum New To JavaReplies: 5Last Post: 11-18-2012, 07:59 PM -
setCaretPosition not working properly
By phyzix5761 in forum AWT / SwingReplies: 6Last Post: 11-17-2012, 09:11 AM -
NetBeans IDE not working properly
By farmer in forum NetBeansReplies: 2Last Post: 12-09-2011, 03:12 PM -
Why this block is not working properly?
By juhiswt in forum New To JavaReplies: 2Last Post: 03-13-2011, 11:08 AM -
Log4j not working properly....
By prakash_dev in forum Advanced JavaReplies: 0Last Post: 03-17-2008, 12:13 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks