[英]How to remove the last character from a multiline string?
示例: 考慮如下的字符串:
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
你可以使用正則表達式:
String output = a.replaceAll(",\\s*\n", "\n");
它將替換逗號的任何序列,0或更多空格以及僅帶換行符的換行符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.