示例: 考虑如下字符串: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 回答
www说
TA贡献1775条经验 获得超8个赞
您可以使用正则表达式:
String output = a.replaceAll(",\\s*\n", "\n");
它将仅用换行符替换逗号、0 个或多个空格和换行符的任何序列。
添加回答
举报
0/150
提交
取消