字符串切割

package mapreduce;

import java.util.HashMap;
import java.util.StringTokenizer;

/**
 * 单词切割
 * @author   IT_BULL                      
 * @Date     2015年7月4日      
 */
public class WordSplite {
    
    public static void main(String[] args) {

        String string = "hello world    hello    china    hello   hadoop hello   mapReduce";
        String word = new String();
        HashMap<String,Integer> map = new HashMap<>();
        //字符串切割,按照空格
        StringTokenizer t = new StringTokenizer(string);
        while(t.hasMoreTokens()) {
            String s = t.nextToken();
            map.put(s, 1);//重复数据不能添加
            System.out.println(s);
        }
        
        System.out.println("=======================>");
        for (String ss : map.keySet()) {
             System.out.println(ss +  map.get(ss));
        }
    }
    

    public static void main1(String[] args) {

        String string = "hello world    hello    china    hello   hadoop hello   mapReduce";
        String[] split = string.split(" ");
        //切割后还有空格样式难看
        for (String s : split) {
            System.out.println(s);
        }
    }
}

 

posted @ 2018-07-04 20:39  马鞍山  阅读(102)  评论(0编辑  收藏  举报