Chapter16e.java
Select all
/* Chater16e.java CIS 260 David Klick 2016-02-15 This program demonstrates two ways to tokenize a String. One uses the traditional StringTokenizer. The other uses the split method of the String class, which returns a String array of tokens. One big advantage of the String.split method is that it accepts regular expressions, so you can specify quite complex delimiters, which could be words instead of just single characters. */ import java.util.StringTokenizer; public class Chapter16e { public static void main(String[] args) { String str = "The lazy, dog jumped? over the - quick brown fox."; StringTokenizer tok = new StringTokenizer(str, " ,?.-"); System.out.println("Using StringTokenizer"); while (tok.hasMoreTokens()) { System.out.println(tok.nextToken()); } System.out.println("\nUsing String.split(pattern)"); for (String s : str.split("[- ,?.]+")) System.out.println(s); } }