Java 使用分隔符拆分\n

Java 使用分隔符拆分\n,java,android,Java,Android,使用此代码时,我需要拆分分隔符为\n的字符串: String delimiter = "\n"; String[] temp; temp = description2[position].split(delimiter); for (int i = 0; i < temp.length; i++) { holder.weeklyparty_text3.setSingleLine(false); holder.weeklyparty_text3.setText(temp[i]

使用此代码时,我需要拆分分隔符为
\n
的字符串:

String delimiter = "\n";
String[] temp;
temp = description2[position].split(delimiter);
for (int i = 0; i < temp.length; i++) {
    holder.weeklyparty_text3.setSingleLine(false);
    holder.weeklyparty_text3.setText(temp[i]);
}
字符串分隔符=“\n”;
字符串[]温度;
temp=description2[position]。拆分(分隔符);
对于(int i=0;i

但是不能从
\n

中获取拆分字符串。您需要转义分隔符字符串中的反斜杠:
“\\n”
拆分使用正则表达式-因此要在换行符上拆分,应使用:

String delimiter = "\\n";

为了支持Unix和Windows新行,请使用:

字符串行[]=String.split(“\r?\n”)

如中所述:


我从未这样做过,但我建议您尝试字符串分隔符=“\\n”;我已经试过了,但仍然没有得到你想要的输入和输出是什么?01-26 02:24:32.611:INFO/System.out(418):\n开放式社交俱乐部\nBar Basque\nCrimson\nForum\nLexicon\nString tempStr=“\n开放式社交俱乐部\nBar Basque\nCrimson\nForum\nLexicon\n”;字符串[]splitArr=tempStr.split(“\n”);for(int i=0;i)列出用于填充description2字符串数组的代码。例如,如果使用读取行方法,则数据中可能没有\n。