Skip to content Skip to sidebar Skip to footer

New Line On Comma Within String

I have an EditText view which will allow the user to edit an address field. I want any text with a comma before it to be put on a new line so for example the following: Some St., S

Solution 1:

Perhaps you could perform a String.replace() to replace all commas with ,\n

so,

Strings="Some St., Some City, Some post code"
 s = s.replace(",",",\n");

You then might have to do something to remove the whitespace

Alternatively, to remove all whitespace:

String s = "Some St., Some City, Some post code";
    String strings[] = s.split(",");

      for(int i = 0; i < strings.length; i++){
         strings[i] = strings[i].trim();
         strings[i] += ",\n";
      }
      s = "";

      for(int i = 0; i < strings.length; i++)
        s += strings[i];

Post a Comment for "New Line On Comma Within String"