StringTokenizer類的countTokens()方法計算在此方法生成任何進一步的異常之前可以調用此令牌化程序的nextToken方法的次數。
注意:在此過程中,當前位置不前進。
用法:
public int countTokens()
參數:該方法不帶任何參數。
返回值:該方法用於使用當前定界符集返回字符串中剩餘的令牌數。
下麵的程序說明StringTokenizer的countTokens()方法的用法:
示例1:
// Java code to illustrate countTokens() method
import java.util.*;
public class StringTokenizer_Demo1 {
public static void main(String args[])
{
// Creating a StringTokenizer
StringTokenizer str_arr
= new StringTokenizer(
"Lets practice at GeeksforGeeks");
// Counting the tokens
int count = str_arr.countTokens();
System.out.println("Total number of Tokens: "
+ count);
// Print the tokens
for (int i = 0; i < count; i++)
System.out.println("token at [" + i + "] : "
+ str_arr.nextToken());
}
}
輸出:
Total number of Tokens: 4 token at [0] : Lets token at [1] : practice token at [2] : at token at [3] : GeeksforGeeks
示例2:
// Java code to illustrate countTokens() method
import java.util.*;
public class StringTokenizer_Demo2 {
public static void main(String args[])
{
// Creating a StringTokenizer
StringTokenizer str_arr
= new StringTokenizer(
"Welcome to GeeksforGeeks");
// Counting the tokens
int count = str_arr.countTokens();
System.out.println("Total number of Tokens: "
+ count);
// Print the tokens
for (int i = 0; i < count; i++)
System.out.println("token at [" + i + "] : "
+ str_arr.nextToken());
}
}
輸出:
Total number of Tokens: 3 token at [0] : Welcome token at [1] : to token at [2] : GeeksforGeeks
相關用法
- Java StringTokenizer hasMoreTokens()用法及代碼示例
- Java StringTokenizer nextElement()用法及代碼示例
- Java StringTokenizer hasMoreElements()用法及代碼示例
- Java StringTokenizer nextToken()用法及代碼示例
- Java Java.util.Collections.rotate()用法及代碼示例
- Java Java.util.Collections.disjoint()用法及代碼示例
- Java Java lang.Long.byteValue()用法及代碼示例
- Java Java lang.Long.reverse()用法及代碼示例
- Java Java lang.Long.highestOneBit()用法及代碼示例
- Java Java lang.Long.numberOfLeadingZeros()用法及代碼示例
- Java Java lang.Long.numberOfTrailingZeros()用法及代碼示例
- Java Java lang.Long.builtcount()用法及代碼示例
注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 StringTokenizer countTokens() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。