写在前面

因为最近在接触hadoop的东西,看示例WordCount的时候里面有一个StringTokenizer的东西特地看了一下

【Java基础】StringTokenizer用法_干货

The string tokenizer class allows an application to break a string into tokens.

一、构造方法

StringTokenizer一共有如下三个构造方法

【Java基础】StringTokenizer用法_干货_02

二、三种方法使用示例

 1 package com.slp;
 2 
 3 import java.util.StringTokenizer;
 4 
 5 public class StringTokenizerTest {
 6 
 7     /**
 8      * @Description: StringTokenizer解释
 9      * @param @param args   
10      * @return void  
11      * @throws
12      * @author liping.sang
13      * @date 2017-6-21
14      */
15     public static void main(String[] args) {
16         StringTokenizer st1 = new StringTokenizer("www.dream-to-pku.com");
17         while(st1.hasMoreTokens()){
18             System.out.print("token:"+st1.nextToken()+"     ");
19             
20         }
21         System.out.println();
22         System.out.println("----------------------------------------");
23         StringTokenizer st2 = new StringTokenizer("www.dream-to-pku.com","-");
24         while(st2.hasMoreTokens()){
25             System.out.print("token:"+st2.nextToken()+"     ");
26         }
27         System.out.println();
28         System.out.println("----------------------------------------");
29         StringTokenizer st3 = new StringTokenizer("www.dream-to-pku.com","-",true);
30         while(st3.hasMoreTokens()){
31             System.out.print("token:"+st3.nextToken()+"     ");
32         }
33     }
34 
35 }
1 token:www.dream-to-pku.com     
2 ----------------------------------------
3 token:www.dream     token:to     token:pku.com     
4 ----------------------------------------
5 token:www.dream     token:-     token:to     token:-     token:pku.com     

三、StringTokenizer常用的方法hasMoreTokens(),nextToken()

【Java基础】StringTokenizer用法_干货_03

StringTokenizer实现了Enumeration<Object>,所以这两个方法是实现枚举类型而进行重写的。