以下文章提供了 Java 中 StringTokenizer 的概述。 java 中的字符串分词器允许应用程序根据某些分隔符将给定字符串分解为标记。字符串的每个分割部分称为一个标记。字符串标记生成器在内部使用 String 类的 substring 方法来创建标记。字符串分词器在内部维护最后一个分词的索引,并根据该索引计算下一个分词。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
在本文中,我们将看到字符串分词器类中可用的不同构造函数的详细描述。此外,还会有 Java 代码示例,展示字符串分词器实例的创建以及其中可用的不同方法的用法。
这是java中字符串分词器的声明:
public class StringTokenizer extends Object implements Enumeration<Object>
字符串分词器是旧框架的一部分。
以下是字符串分词器类的主要构造函数。
可以根据需要使用上面指定的构造函数。
Method Name | Description |
boolean hasMoreTokens() | This method checks whether there are more tokens available. |
String nextToken() | This method returns the value of the next available token. |
String nextToken(String delim) | This method returns the value of the next available token based on provided delimiter. |
boolean hasMoreElements() | This works similarly to hasMoreTokens. |
Object nextElement() | This method is the same as nextToken but returns an object. |
int countTokens() | This method returns a number of tokens. |
Given below are the examples:
Let us see an example of a string tokenizer class showing the use of the first constructor.
Code:
package com.edubca.stringtokenizerdemo; import java.util.*; public class StringTokenizerDemo{ public static void main(String args[]){ //create string tokenizer instance StringTokenizer st1 = new StringTokenizer("This is Edubca Java Training"); System.out.println("Tokens separated by space are : "); while (st1.hasMoreTokens()) System.out.println(st1.nextToken()); } } <strong> Output:</strong>
In this example, we will see the use of the second constructor of a string tokenizer class that accepts a string and delimiter.
Code:
package com.edubca.stringtokenizerdemo; import java.util.*; public class StringTokenizerDemo{ public static void main(String args[]){ //create string tokenizer instance StringTokenizer st1 = new StringTokenizer("This,is,Edubca,Java,Training", ","); System.out.println("Tokens separated by comma are : "); while (st1.hasMoreTokens()) System.out.println(st1.nextToken()); } }
Output:
In the above example, we have seen how to create tokens based on a given delimiter in the string tokenizer.
In this example, we will see the use of the third constructor of a string tokenizer class that accepts a string, delimiter and boolean value.
Code:
package com.edubca.stringtokenizerdemo; import java.util.*; public class StringTokenizerDemo{ public static void main(String args[]){ //create string tokenizer instance StringTokenizer st1 = new StringTokenizer("This,is,Edubca,Java,Training", ",",true); System.out.println("Tokens separated by comma are : "); while (st1.hasMoreTokens()) System.out.println(st1.nextToken()); } }
Output:
As we can see in the above output delimiter is also considered as a token.
In this example, we will how to handle multiple delimiters in java string tokenizer.
Code:
package com.edubca.stringtokenizerdemo; import java.util.*; public class StringTokenizerDemo{ public static void main(String args[]){ String stringvalue = "http://127.0.0.1:8080/"; //create string tokenizer instance with multiple delimiters StringTokenizer st1=new StringTokenizer (stringvalue,"://."); System.out.println("Tokens generated are : "); while (st1.hasMoreTokens()) System.out.println(st1.nextToken()); } }
Here is the output produced after running the above code:
The above tokens are generated by tokenizing strings based on multiple tokens (://.).
In this example, we will see the use of the count tokens method in the string tokenizer.
Code:
package com.edubca.stringtokenizerdemo; import java.util.*; public class StringTokenizerDemo{ public static void main(String args[]){ //create string tokenizer instance StringTokenizer st1 = new StringTokenizer("This,is,Edubca,Java,Training", ",",true); System.out.println("Number of available tokens are : " + st1.countTokens()); System.out.println("Tokens separated by comma are : "); while (st1.hasMoreTokens()) System.out.println(st1.nextToken()); } }
Output:
From the above discussion, we have a clear understanding of what is string tokenizer in java, how it’s created, and what are the different methods available in the string tokenizer class.
以上是Java 中的 StringTokenizer的详细内容。更多信息请关注PHP中文网其他相关文章!