1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
/* Java StringTokenizer count tokens example This example shows how to count remaining tokens of java StringTokenizer object using countTokens() method. */ import java.util.StringTokenizer; public class StringTokenizerCountTokensExample { public static void main(String[] args) { //create StringTokenizer object StringTokenizer st = new StringTokenizer("Java StringTokenizer count Tokens Example"); /* countTokens() method returns the number of tokens remaining from the current delimiter set. It calculates the number of times StringTokenizer's nextToken() method can be called before it generates an exception. Please note that invoking countTokens() method does not advance the current position. */ //iterate through tokens while(st.hasMoreTokens()){ System.out.println("Remaining Tokens : " + st.countTokens()); System.out.println(st.nextToken()); } } } /* Output Would be Remaining Tokens : 5 Java Remaining Tokens : 4 StringTokenizer Remaining Tokens : 3 count Remaining Tokens : 2 Tokens Remaining Tokens : 1 Example */ |
add some special cases