Replace comma with newline in java
Simply use following code:
String emailList="AA,BB,CC";
emailList=emailList.replaceAll(",", "\n");
System.out.println(emailList);
Output
AA
BB
CC
Now based on above your code, your method looks like following:
public String getFormattedEmails(String emailList) {
String emails=emailList.replaceAll(",", "\n");
return emails;
}
Hope it helps:
String emails = emailList.replaceAll(",", "\n");
you can use Scanner
too
String emails = "AA,BB,CC"
String emailsNew = replaceCommas(emails);
String replaceCommas(String a){
StringBuilder result = new StringBuilder();
Scanner scan = new Scanner(a);
scan.useDelimiter(",");
while(scan.hasNext()){
result.append(scan.next());
result.append("\n");
}
return result.toString();
}
System.out.println(emailsNew);
will print:
AA
BB
CC