以下文章提供了 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 無盡。

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

Dreamweaver Mac版
視覺化網頁開發工具

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。