我尝试使用string.split("\n\n"),但它不起作用。有人有解决办法吗?提前感谢。
首先,您应该像这样使用另一个\
转义\
:
string.split("\\n\\n");
另一种方法是使用系统默认的换行符:
string.split(System.getProperty("line.separator")+"{2}");
string.split("(\\r\\n|"+System.getProperty("line.separator")+")+");
split
需要正则表达式,因此您可以尝试使用变体来解决问题。
不要忘记,有时换行符不仅仅是\n
符号,对于Windows文件,它可能是\r\n
字符序列。
您应该使用另一个 \
进行转义,因此请尝试:-
string.split("\\n\\n");
\
:string.split("\\n\\n");