public class Test { public static void main(String[] args) { String s = "ni hao ma he yi yong"; String[] strs = s.split("\n| "); for(String string : strs) { System.out.println(string); } } }
一个字符串同时匹配换行和空格,最终输出:
nihaomaheyiyong