Java String Tokens

  • + 3 comments

    It is much easier (and less code) to use the StringTokenizer class. Using Collections is overkill in this instance. You can easily set the delimeters in the constructor:

    StringTokenzier st = new StringTokenizer(input, "A-Z !,?._'@");
    

    Then you can call the appropriate methods:

    st.countTokens();
    
    // iterate through your StringTokenizer object
    while (st.hasMoreTokens()) {
        System.out.println(st.nextToken());
    }
    

    https://docs.oracle.com/javase/8/docs/api/java/util/StringTokenizer.html

    Regards, Smyhk