以下文章提供了 Java 中 StringTokenizer 的概述。 java 中的字符串分词器允许应用程序根据某些分隔符将给定字符串分解为标记。字符串的每个分割部分称为一个标记。字符串标记生成器在内部使用 String 类的 substring 方法来创建标记。字符串分词器在内部维护最后一个分词的索引,并根据该索引计算下一个分词。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
在本文中,我们将看到字符串分词器类中可用的不同构造函数的详细描述。此外,还会有 Java 代码示例,展示字符串分词器实例的创建以及其中可用的不同方法的用法。
这是java中字符串分词器的声明:
public class StringTokenizer extends Object implements Enumeration<object></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. |
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>
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.
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:
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:
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:
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中文网其他相关文章!

本文讨论了使用Maven和Gradle进行Java项目管理,构建自动化和依赖性解决方案,以比较其方法和优化策略。

本文使用Maven和Gradle之类的工具讨论了具有适当的版本控制和依赖关系管理的自定义Java库(JAR文件)的创建和使用。

本文讨论了使用咖啡因和Guava缓存在Java中实施多层缓存以提高应用程序性能。它涵盖设置,集成和绩效优势,以及配置和驱逐政策管理最佳PRA

本文讨论了使用JPA进行对象相关映射,并具有高级功能,例如缓存和懒惰加载。它涵盖了设置,实体映射和优化性能的最佳实践,同时突出潜在的陷阱。[159个字符]

Java的类上载涉及使用带有引导,扩展程序和应用程序类负载器的分层系统加载,链接和初始化类。父代授权模型确保首先加载核心类别,从而影响自定义类LOA


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

mPDF
mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

WebStorm Mac版
好用的JavaScript开发工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器