[英]How to remove the last character from a multiline string?
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
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. 它将替换逗号的任何序列,0或更多空格以及仅带换行符的换行符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.