如何从多行字符串中删除最后一个字符?

时间:2018-08-15 13:04:35

标签: java string multilinestring

示例: 考虑如下字符串:

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

输出:

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

由于这是多行字符串,我如何才能从输出中删除每行最后出现的逗号并打印输出,如下所述?

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

1 个答案:

答案 0 :(得分:8)

您可以使用正则表达式:

Thread.sleep(60000);

它将仅用换行符替换逗号,0个或多个空格和换行符的任何序列。