vendredi 3 avril 2015

How to insert a "," in between each char of a string

Im at a point in my program where I need to set marks in a string before I pass it through another method, I have it so that every 4th char will have a "|" inserted, this is to mark a row break. Not I want to take each char in between the outter marks "|" and put a ",". that two char array method wont work here otherwise I would have tryed to use that but I'm not looking for a char array.



public static String matrixFormatter(String x){

x = x.substring(0, 4) + "|" + x.substring(4, x.length());
return x;
}


this works so far, now I want to add a "," between each char, I thought the code below would work and this would be easy but I was wrong.



public static String matrixFormatter(String x){

for(int i = 0; i<=x.length(); i+=4){
for(int j = 0; j<=x.length(); i++){
x = x.substring(0, i) + "|" + x.substring(i, x.length());
x = x.substring(0, j) + "|" + x.substring(j, x.length());
}
}
return x;


}


Aucun commentaire:

Enregistrer un commentaire