sar*_*rav 2 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);
Run Code Online (Sandbox Code Playgroud)
输出:
This line1 has a,b,c,
This line2 has d,e,f,
This line3 has g,h,i
Run Code Online (Sandbox Code Playgroud)
由于这是一个多行字符串,我如何能够从输出中删除每行最后出现的逗号并打印输出,如下所述?
This line1 has a,b,c
This line2 has d,e,f
This line3 has g,h,i
Run Code Online (Sandbox Code Playgroud)
你可以使用正则表达式:
String output = a.replaceAll(",\\s*\n", "\n");
Run Code Online (Sandbox Code Playgroud)
它将替换逗号的任何序列,0或更多空格以及仅带换行符的换行符.
| 归档时间: |
|
| 查看次数: |
108 次 |
| 最近记录: |