-
Notifications
You must be signed in to change notification settings - Fork 0
/
str1_tkn.java
49 lines (38 loc) · 2.23 KB
/
str1_tkn.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.StringTokenizer;
class str1_tkn {
public static void main(String[] a){
// String Tokenizer Constructors
// 1. Using first method of Constructors
System.out.println("Using first Method of String Tokenizer : ");
StringTokenizer str1 = new StringTokenizer("Hello World !"," ");
// Printing the Tokens
while(str1.hasMoreTokens())
System.out.println(str1.nextToken());
// 2. Using Second Method of Constructors
System.out.println("Using Second Method of String Tokenizer : ");
StringTokenizer str2 = new StringTokenizer("This : was : the : good : one : !","!");
// Printing the Tokens
while(str2.hasMoreTokens())
System.out.println(str2.nextToken());
// 3. Using Third Way of Constructors
System.out.println("Using Third Method of String Tokenizer : ");
StringTokenizer str3 = new StringTokenizer("Join : this : String",":",true);
// String Tokens
while(str3.hasMoreTokens())
System.out.println(str3.nextToken());
// Some Methods of String Tokenizer Class
// 1. countTokens() : Used to count the number of Token Generated by the StringTokenizer
int count = str1.countTokens();
System.out.println("Number of Token Generated in the first String : " + count);
// 2. hasMoreTokens() : Used to check if the Tokens are present in the StringTokenizer's String
// Above method is used in the StringTokenizer's String's Created above while iteration of the while Loop
// 3. nextToken() : Returns the next Token from the given String Tokenizer Class
// Above method is already been implement together with the hasMoreTokens() in the while Loop
// 4. hasMoreElements() : Works similar to the hasMoreToken() method.
StringTokenizer str4 = new StringTokenizer("This is not a good one !"," ");
while(str4.hasMoreElements())
System.out.println(str4.nextElement());
// 5. nextElement() : It is used in order to point out and return the next element or partition of the String
// And this method works similar to the nextToken() method, together with its example is been given above.
}
}