Yes, I know Java's Strings are immutable, but still, that alone does not explain why this won't work for me...
I ran into this problem when I tried to convert an array of strings (String[]) to one string of the form: ('String[0]','String[1]',...,'String[n]').
For example, given the following array:
String[] array=new String[3];
array[0]="Jake";
array[1]="Mishel";
array[2]="Dan";
I wanted to create the string: ('Jake','Mishel','Dan')
But, the way to do it is not the purpose of this post...
What I'm interested to know is, why does this work:
String[] array=new String[3];
array[0]="Jake";
array[1]="Mishel";
array[2]="Dan";
for (int i=0; i<array.length; i++) {
array[i]=array[i]+"'";
array[i]="'"+array[i];
}
String str=Arrays.toString(array);
str=str.replace('[','(');
str=str.replace(']',')');
System.out.println(str); //produces ('Jake','Mishel','Dan'), as desired
But this doesn't:
String[] array=new String[3];
array[0]="Jake";
array[1]="Mishel";
array[2]="Dan";
for (String str: array) {
str=str+"'";
str="'"+str;
}
String str=Arrays.toString(array);
str=str.replace('[','(');
str=str.replace(']',')');
System.out.println(str); //produces wrong output: (Jake,Mishel,Dan)
?