3

Example: Consider a String like below:

String a="This line1 has a,b,c,\nThis line2 has d,e,f, \nThis line3 has g,h,i";
System.out.println(a);

Output:

This line1 has a,b,c,                 
This line2 has d,e,f,                        
This line3 has g,h,i

As this is a multiline string, How will I be able to remove last occurring comma of each line here from the output & print an output like below below mentioned?

This line1 has a,b,c                
This line2 has d,e,f                        
This line3 has g,h,i
sarav
  • 47
  • 9

1 Answers1

10

You could use a regex:

String output = a.replaceAll(",\\s*\n", "\n");

It will replace any sequence of a comma, 0 or more spaces and a newline with just a newline character.

assylias
  • 321,522
  • 82
  • 660
  • 783