public String[] split(String regex) 預設limit為0
public String[] split(String regex, int limit)
當limit>0時,則應用n-1次
public static void main(String[] args) { String s = "boo:and:foo"; String[] str = s.split(":",2); System.out.print(str[0] + "," + str[1]); }當limit>0時,則應用n-1次
public static void main(String[] args) { String s = "boo:and:foo"; String[] str = s.split(":",-2); for(int i = 0 ; i < str.length ; i++){ System.out.print(str[i] + " "); } }
當limit
public static void main(String[] args) { String s = "boo:and:foo"; String[] str = s.split("o",-2); for(int i = 0 ; i < str.length ; i++){ if( i < str.length - 1) System.out.print("(" + str[i] + "),"); else System.out.print("(" + str[i] + ")"); } }
結果:
boo and foo
當無限次字符串=0時,應用無限次字符串並省略末尾的空字符串
public static void main(String[] args) { String s = "boo:and:foo"; String[] str = s.split("o",0); for(int i = 0 ; i < str.length ; i++){ if( i < str.length - 1) System.out.print("(" + str[i] + "),"); else System.out.print("(" + str[i] + ")"); } }結果:(b),(),(:and:f),(),()
rrreee
結果:(b),(),(:andand)以上就是對Java split 的資料整理,後續繼續補充相關資料,謝謝大家對本站的支持!
更多java split用法詳解及實例代碼相關文章請關注PHP中文網!