Java の StringTokenizer

WBOY
リリース: 2024-08-30 15:13:21
オリジナル
822 人が閲覧しました

次の記事では、Java の StringTokenizer の概要を説明します。 Java の String Tokenizer を使用すると、アプリケーションは特定の文字列を区切り文字に基づいてトークンに分割できます。文字列の各分割部分はトークンと呼ばれます。文字列トークナイザーは、内部で String クラスの部分文字列メソッドを使用してトークンを作成します。文字列トークナイザーは内部的に最後のトークンのインデックスを維持し、このインデックスに基づいて次のトークンを計算します。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

この記事では、文字列トークナイザー クラスで使用できるさまざまなコンストラクターについて詳しく説明します。さらに、文字列トークナイザー インスタンスの作成とそこで利用可能なさまざまなメソッドの使用法を示す Java コード例もあります。

Java での文字列トークナイザーの宣言は次のとおりです。

public class StringTokenizer extends Object
implements Enumeration<Object>
ログイン後にコピー

文字列トークナイザー コンストラクター

String Tokenizer はレガシー フレームワークの一部です。

以下は、文字列トークナイザー クラスの主なコンストラクターです。

  • StringTokenizer (String str): これは、指定された文字列の文字列トークナイザーを作成します。これにより、スペース、タブ、改行、キャリッジリターン文字、フォームフィード文字などのデフォルトの区切り文字が設定されます。
  • StringTokenizer (String str, String delim): これにより、指定された文字列の文字列トークナイザーが作成され、指定された区切り文字に基づいてトークンが生成されます。
  • StringTokenizer (String str, String delim, boolean returndelims): これは、指定された文字列の文字列トークナイザーを作成し、指定された区切り文字に基づいてトークンが生成されます。 3 番目のパラメーターは、トークンとして区切り文字が必要かどうかを指定するブール値です。

上記で指定したコンストラクターは、要件に応じて使用できます。

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.
メソッド名 説明 ブール値 hasMoreTokens() このメソッドは、さらに利用可能なトークンがあるかどうかを確認します。 String nextToken() このメソッドは、次に利用可能なトークンの値を返します。 String nextToken(String delim) このメソッドは、指定された区切り文字に基づいて、次に利用可能なトークンの値を返します。 ブール値 hasMoreElements() これは 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 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!