Important methods of StringBuffer class are mention below
Method | Description |
---|---|
hasMoreTokens() | It checks to see if there are any additional tokens available. |
String nextToken(String delim) | Based on the delimiter, it returns the next token. |
String nextToken() | It returns the StringTokenizer object's next token. |
Object nextElement() | It is similar to nextToken(), but the return type is Object. |
int countTokens() | The total number of tokens is returned. |
StringTokenizer class on basis of whitespace.
// Java example of String length() Methods
import java.util.StringTokenizer; public class Main { public static void main(String args[]){ StringTokenizer st = new StringTokenizer("I Love India"," "); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); } } }
Output:
I
Love
India
The hasMoreTokens() function of the StringTokenizer class produces identical results to the hasMoreElements function. The only difference lies in the fact that the Enumeration interface can be utilized for implementation.
// Java example of StringTokenizer hasMoreElements() Method
import java.util.StringTokenizer; public class Main { public static void main(String args[]){ StringTokenizer st = new StringTokenizer("I Love India"," "); while (st.hasMoreElements()){ System.out.println(st.nextToken()); } } }
Output:
I
Love
India
The tokenizer String's total number of tokens is determined by the countTokens method.
// Java example of StringTokenizer countTokens() Method
import java.util.StringTokenizer; public class Main { public static void main(String args[]){ StringTokenizer st = new StringTokenizer("I Love India"," "); System.out.println("Total Tokens: "+st.countTokens()); } }
Output:
Total Tokens: 3
The next token object in the tokenizer String is returned by nextElement(). It can use the interface for enumeration.
// Java example of StringTokenizer nextElement Method
import java.util.StringTokenizer; public class Main { public static void main(String args[]){ StringTokenizer st = new StringTokenizer("I Love India"," "); while (st.hasMoreTokens()){ // it check for more tokens System.out.println(st.nextElement()); } } }
Output:
I
Love
India
Police Colony
Patna, Bihar
India
Email:
Post your comment