当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java StringTokenizer hasMoreTokens()用法及代码示例


StringTokenizer类的hasMoreTokens()方法检查此StringTokenizer是否还有可用的令牌。

用法:

public boolean hasMoreTokens()

参数:该方法不带任何参数。


返回值:如果在当前位置之后在字符串中找到至少一个以上的令牌,则该方法返回boolean True。

下面的程序说明StringTokenizer的hasMoreTokens()方法的用法:
示例1:

// Java code to illustrate hasMoreTokens() method 
  
import java.util.*; 
  
public class StringTokenizer_Demo { 
    public static void main(String args[]) 
    { 
        // Creating a StringTokenizer 
        StringTokenizer str_arr 
            = new StringTokenizer( 
                "Lets practice at GeeksforGeeks"); 
  
        // Counting the tokens 
        System.out.println("The number of Tokens are: "
                           + str_arr.countTokens()); 
  
        // Checking for any tokens 
        System.out.println(str_arr.hasMoreTokens()); 
  
        // Checking and displaying the Tokens 
        while (str_arr.hasMoreTokens()) { 
            System.out.println("The Next token: "
                               + str_arr.nextToken()); 
        } 
    } 
}
输出:
The number of Tokens are: 4
true
The Next token: Lets
The Next token: practice
The Next token: at
The Next token: GeeksforGeeks

示例2:

// Java code to illustrate hasMoreTokens() method 
  
import java.util.*; 
  
public class StringTokenizer_Demo { 
    public static void main(String args[]) 
    { 
        // Creating a StringTokenizer 
        StringTokenizer str_arr 
            = new StringTokenizer(""); 
  
        // Counting the tokens 
        System.out.println("The number of Tokens are: "
                           + str_arr.countTokens()); 
  
        // Checking for any tokens 
        System.out.println(str_arr.hasMoreTokens()); 
    } 
}
输出:
The number of Tokens are: 0
false


相关用法


注:本文由纯净天空筛选整理自Chinmoy Lenka大神的英文原创作品 StringTokenizer hasMoreTokens() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。