How to remove the last character from a multiline string?

sarav :

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
assylias :

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=111719&siteId=1