Java中split()函式的用法及一些注意細節
阿新 • • 發佈:2019-01-02
String.split("要切割的準側")返回的是一個String[ ]的首地址;String.split("要切割的準側").length 返回的是這個String被切割後的子字串的個數(及被切割成了幾個段);String.split(""),此時,切割後的第一個段是空字串。程式碼如下:
package Demo; public class DemoSplit { public static void main(String[] args) { test(); } public static void test(){ String s="a,b,c,d,e"; String temp[]; temp=s.split(",");//String用split切割後,返回的是一個String陣列。 System.out.println("temp==="+temp);//System.out.print(s.split("要切割的準則"))返回的是字串陣列的首地址 System.out.println("之後的長度:"+temp.length); System.out.println("切割後,子段的內容為:"); for(int i=0;i<temp.length;i++){ System.out.println(temp[i]); } String temp1[]; temp1=s.split(""); System.out.println("temp1==="+temp1);//System.out.print(s.split("要切割的準則"))返回的是字串陣列的首地址 System.out.println("之後的長度:"+temp1.length); System.out.println("切割後,子段的內容為:"); for(int i=0;i<temp1.length;i++){ System.out.println(temp1[i]); } } }
執行結果為: