Question:
How can you split a String into tokens?
Answer:
By using a StringTokenizer
Code:
import java.util.StringTokenizer; public class SplitString { public static void main(String[] args) { String str = "Paul:John:George:Ringo"; // using : as a delimiter StringTokenizer st = new StringTokenizer(str, ":"); System.out.println(str); while (st.hasMoreElements()) { System.out.println(st.nextElement()); } } }
Output:
$ java SplitString Paul:John:George:Ringo Paul John George Ringo