creates a set of unique string tokens which are separated by separator - Java java.lang

Java examples for java.lang:String Tokenize

Description

creates a set of unique string tokens which are separated by separator

Demo Code


import java.security.MessageDigest;

public class Main{
    public static void main(String[] argv){
        String str = "java2s.com";
        String separator = ".";
        System.out.println(getUniqueTokens(str,separator));
    }// w w w . j a v a 2  s .c  o m
    /**  
     * This method creates a set of unique string tokens which are separated by  
     * separator  
     *  
     * @param str  
     * @param separator  
     * @return  
     */
    public static Set getUniqueTokens(String str, String separator) {
        StringTokenizer tokenizer = new StringTokenizer(str, separator);
        Set tokens = new HashSet();
        while (tokenizer.hasMoreTokens()) {
            tokens.add(tokenizer.nextToken());
        }
        return tokens;
    }
}

Related Tutorials