将字符串拆分为逐行分隔符?

时间:2015-04-28 01:53:53

标签: java string-split

对于字符串

String cow = "A cow jumped \n over the \n moon."

如何将字符串拆分为列表?

List<String> test = new ArrayList<String>();
Collections.addAll(test, cow).split("\n")); 

上面的代码无效。

1 个答案:

答案 0 :(得分:5)

试试这个:

String cow = "A cow jumped \n over the \n moon.";
List<String> test = Arrays.asList(cow.split("\n"));