複数行の文字列から最後の文字を削除する方法?

sarav:

例: 以下のような文字列を考えてみます。

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

あなたは正規表現を使用することができます。

String output = a.replaceAll(",\\s*\n", "\n");

これは、カンマ、0以上のスペースだけ改行文字と改行のいずれかの配列を置換します。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=222537&siteId=1
おすすめ