Artikel berikut menyediakan garis besar untuk StringTokenizer di Jawa. String Tokenizer dalam java membenarkan aplikasi untuk memecahkan rentetan yang diberikan kepada token berdasarkan beberapa pembatas. Setiap bahagian Split rentetan dipanggil token. Tokenizer rentetan secara dalaman menggunakan kaedah subrentetan kelas String untuk mencipta token. String tokenizer secara dalaman mengekalkan indeks token terakhir dan mengira token seterusnya berdasarkan indeks ini.
Mulakan Kursus Pembangunan Perisian Percuma Anda
Pembangunan web, bahasa pengaturcaraan, ujian perisian & lain-lain
Dalam artikel ini, kita akan melihat penerangan terperinci tentang pembina berbeza yang tersedia dalam kelas tokenizer rentetan. Di samping itu, akan terdapat contoh kod java yang menunjukkan penciptaan contoh tokenizer rentetan dan penggunaan kaedah berbeza yang tersedia di dalamnya.
Berikut ialah pengisytiharan tokenizer rentetan dalam java:
public class StringTokenizer extends Object implements Enumeration<Object>
String Tokenizer ialah sebahagian daripada rangka kerja Legasi.
Berikut ialah pembina utama Kelas tokenizer rentetan.
Pembina yang dinyatakan di atas boleh digunakan bergantung pada keperluan.
|
Penerangan | ||||||||||||||
boolean hasMoreTokens() | Kaedah ini menyemak sama ada terdapat lebih banyak token yang tersedia. | ||||||||||||||
String nextToken() | Kaedah ini mengembalikan nilai token yang tersedia seterusnya. | ||||||||||||||
String nextToken(String delim) | Kaedah ini mengembalikan nilai token yang tersedia seterusnya berdasarkan pembatas yang disediakan. | ||||||||||||||
boolean hasMoreElements() | Ini berfungsi sama dengan hasMoreTokens. | ||||||||||||||
Objek nextElement() | Kaedah ini adalah sama seperti nextToken tetapi mengembalikan objek. | ||||||||||||||
int countTokens() | Kaedah ini mengembalikan beberapa token. |
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.
Atas ialah kandungan terperinci StringTokenizer di Jawa. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!