Android 中StringTokenizer 實現字串分割
阿新 • • 發佈:2019-02-03
StringTokenizer 實現字串分割
1. java.util.StringTokenizer 用於分割字串
例項一:
String str = "abc-de" ;
StringTokenizer st = new StringTokenizer (str , "-") ;
System.out.println( st.nextToKen() ) ;
System.out.println( st.nextToKen() ) ;
輸出:
abc
de
2.用String的split() ;來分割字串,返回一個字串陣列
例項二:
-
package com.struts2.bean;
- publicclass test {
- publicstaticvoid main(String[] args)
- {
- String str = "abc-d";
- String[] s = str.split("-");
- for(int i = 0 ; i < s.length ; i++)
- {
- System.out.println(s[i]);
- }
- }
- }
輸出:
abc
d