📜  Java中的 StringTokenizer nextToken() 方法及示例

📅  最后修改于: 2022-05-13 01:54:31.671000             🧑  作者: Mango

Java中的 StringTokenizer nextToken() 方法及示例

StringTokenizer 类nextToken()方法用于从这个 StringTokenizer 中依次返回下一个令牌。

句法:

public String nextToken()

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

返回值:该方法返回字符串标记器行中存在的下一个标记。

下面的程序说明了 StringTokenizer 的 nextToken() 方法的工作:

示例 1:

// Java code to illustrate nextToken() 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");
  
        // Displaying the Tokens
        while (str_arr.hasMoreTokens()) {
            System.out.println("The Next token: "
                               + str_arr.nextToken());
        }
    }
}
输出:
The Next token: Lets
The Next token: practice
The Next token: at
The Next token: GeeksforGeeks

示例 2:

// Java code to illustrate nextToken() method
  
import java.util.*;
  
public class StringTokenizer_Demo {
    public static void main(String args[])
    {
        // Creating a StringTokenizer
        StringTokenizer str_arr
            = new StringTokenizer(
                "Welcome to GeeksforGeeks");
  
        // Displaying the Tokens
        while (str_arr.hasMoreTokens()) {
            System.out.println("The Next token: "
                               + str_arr.nextToken());
        }
    }
}
输出:
The Next token: Welcome
The Next token: to
The Next token: GeeksforGeeks