首页 > Java > java教程 > 正文

Java 中的 StringTokenizer

WBOY
发布: 2024-08-30 15:13:21
原创
821 人浏览过

以下文章提供了 Java 中 StringTokenizer 的概述。 java 中的字符串分词器允许应用程序根据某些分隔符将给定字符串分解为标记。字符串的每个分割部分称为一个标记。字符串标记生成器在内部使用 String 类的 substring 方法来创建标记。字符串分词器在内部维护最后一个分词的索引,并根据该索引计算下一个分词。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

在本文中,我们将看到字符串分词器类中可用的不同构造函数的详细描述。此外,还会有 Java 代码示例,展示字符串分词器实例的创建以及其中可用的不同方法的用法。

这是java中字符串分词器的声明:

public class StringTokenizer extends Object
implements Enumeration<Object>
登录后复制

字符串分词器构造函数

字符串分词器是旧框架的一部分。

以下是字符串分词器类的主要构造函数。

  • StringTokenizer (String str): 这会为指定的字符串创建一个字符串标记生成器。设置默认分隔符,可以是空格、制表符、换行符、回车符和换页符。
  • StringTokenizer (String str, String delim): 这会为指定的字符串创建一个字符串分词器,并且将根据指定的分隔符生成标记。
  • StringTokenizer (String str, String delim, boolean returndelims): 这会为指定的字符串创建一个字符串分词器,并且将根据指定的分隔符生成标记。第三个参数是一个布尔值,指定是否需要分隔符作为标记。

可以根据需要使用上面指定的构造函数。

Java 中字符串分词器的重要方法

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.
方法名称 描述 布尔值 h​​asMoreTokens() 此方法检查是否有更多可用令牌。 字符串 nextToken() 此方法返回下一个可用令牌的值。 字符串 nextToken(字符串 delim) 此方法根据提供的分隔符返回下一个可用标记的值。 布尔值 h​​asMoreElements() 这与 hasMoreTokens 类似。 对象 nextElement() 此方法与 nextToken 相同,但返回一个对象。 int countTokens() 此方法返回多个标记。 表>

Examples

Given below are the examples:

Example #1

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>
登录后复制

Java 中的 StringTokenizer

Example #2

 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.

 Java 中的 StringTokenizer

Example #3

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:

 Java 中的 StringTokenizer

As we can see in the above output delimiter is also considered as a token.

Example #4

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:

Java 中的 StringTokenizer

The above tokens are generated by tokenizing strings based on multiple tokens (://.).

Example #5

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:

Java 中的 StringTokenizer

Conclusion – StringTokenizer in Java

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中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!