StringTokenizer는 스트링을 Token으로 나누어준다.

예를들어 "this is my string" 이라는 스트링을 this, is, my, string 4개의 스트링으로 끊어주는 기능을 제공한다.


import java.util.StringTokenizer;
 
public class JavaTestMain {
 
    public static void main(String[] args) {
         
        String str = "this is my string";
         
        StringTokenizer st = new StringTokenizer(str);
         
        System.out.println(st.countTokens());
         
        while(st1.hasMoreTokens()) {
            System.out.println(st.nextToken());
        }
         
        System.out.println(st.countTokens());
    }
 
}


결과)

4
this
is
my
string
0



import java.util.StringTokenizer;
 
public class JavaTestMain {
 
    public static void main(String[] args) {
         
        String str = "this,is,my,string";
         
        StringTokenizer st = new StringTokenizer(str,",");
         
        while(st.hasMoreTokens()) {
            System.out.println(st.nextToken());
        }
    }
}


결과)

this
is
my
string



import java.util.StringTokenizer;
 
public class JavaTestMain {
 
    public static void main(String[] args) {
         
        String str = "this,is,my,string";
         
        StringTokenizer st = new StringTokenizer(str,",",true);
         
        while(st.hasMoreTokens()) {
            System.out.println(st.nextToken());
        }
    }
}


결과)

this
,
is
,
my
,
string



import java.util.StringTokenizer;
 
public class JavaTestMain {
 
    public static void main(String[] args) {
         
        String str = "this%^is%^my%^string";
         
        StringTokenizer st = new StringTokenizer(str,"%^");
         
        System.out.println(st.nextToken());
        System.out.println(st.nextToken());
        System.out.println(st.nextToken("%"));
        System.out.println(st.nextToken());
    }
}


결과)

this
is
^my
^string



추가정보 ) http://docs.oracle.com/javase/7/docs/api/java/util/StringTokenizer.html