之前经常遇到切割字符串的情况,这里把常见的场景总结一下:
1 // 以空格切割 2 String s1 = "dew qefdewrfr freqg gtrh yt hy"; 3 String[] words1 = s1.split("\s+"); 4 5 // 以 . 切割 6 String s2 = "dew.qefdewrfr.freqg.gtrh.yt.hy"; 7 String[] words2 = s2.split("\."); 8 9 // 以 * 切割 10 String s3 = "dew*qefdewrfr*freqg*gtrh.yt.hy"; 11 String[] words3 = s3.split("\*"); 12 13 // 以 " 切割 14 String s4 = "dewqe"fdewrfr"freqg.gtrh.yt.hy"; 15 String[] words4 = s4.split("\""); 16 //String[] words44 = s4.split("""); 17 18 // 以普通字符 '(f) 切割 19 String s5 = "dew'qefdewrfr'freqg.gtrh.yt.hy"; 20 String[] words5 = s5.split("'"); 21 String[] words6 = s5.split("f"); 22 23 for(String s: words4) { 24 System.out.println(s); 25 }