Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Sunday, 25 June 2017

StringTokenizer In Java

 Java StringTokenizer

StringTokenizer

The StringTokenizer class allows an application to break a string into tokens(parts). The StringTokenizer class are available into java.util package.

In other words, StringTokenizer class breaks a string on the basis of delimiters(spaces or any special symbols).

for example : suppose there is a word or String "Java is a simple programming language". We can break this string into tokens(part) with the help of delimiters like spaces or any special symbols.

java
is
a
simple
programming
language

The StringTokenizer is a legacy class in java.


Constructors of StringTokenizer Class

There are some constructors of StringTokenizer class, these are

1) StringTokenizer(String str)

This constructor creates a string tokenizer for the specified string.

2) StringTokenizer(String str, String delim)

This constructor creates a string tokenizer for the specified string and delimiter.

3) StringTokenizer(String str, String delim, boolean retrunDelims)

This constructor creates a string tokenizer for the specified string delimiter and return value. If return value is true, delimiter characters are considered to be tokens. If return values is false, delimiter characters serve to separate tokens.


Methods of StringTokenizer Class

There are some certain methods of StringTokenizer class in java which is given below.

1) boolean hasMoreTokens()

This method tests if there are more tokens available from this tokenizer's string.

2) boolean hasMoreElements()

This method returns the same value as the hasMoreTokens method.

3) String nextToken()

This method returns the next token from this string tokenizer.

4) Object nextElement()

This method returns the same value as the nextToken() method, excepts that is declared return value is Object rather than String.

5) String nextToken(String delim)

This method returns the next token in this string tokenizer's string.

6) int countTokens()

This method returns the total numbers of tokens.


Java StringTokenizer Example

This is a simple example of string tokenizer class where we will break or tokenize this string " java is a simple programming language" on the basis of white spaces. Space is the default delimiter.

import java.util.StringTokenizer;
class StringTokenizerExample
{
public static void main(String args[])
{
StringTokenizer st = new StringTokenizer("java is a simple programming language", " ");//using space delimiter
while(st.hasMoreTokens())
{
System.out.println(st.nextToken());
}
}
}

output : java
              is
              a
              simple
              programming
              language



Java StringTokenizer Example 1

This is another example of StringTokenizer, in this example we will break the string on the basis of 2 delimiters " space" and " comma". Space is the default delimiter.

import java.util.StringTokenizer;
public class StringTokenizerExample1
{
public static void main(String args[])
{
String s = "i am john, thank you";
StringTokenizer st = new StringTokenizer(s);//by using space
StringTokenizer st1 = new StringTokenizer(s, ",");//by using comma

System.out.println("tokenize by using space delimiter");

while(st.hasMoreTokens())

{

System.out.println(st.nextToken());

}

System.out.println("tokenize by using comma delimiter");


while(st1.hasMoreTokens())
{
System.out.println(st1.nextToken());
}
}
}

output : tokenize by using space delimiter
              i
              am
              john,
              thank
              you
              tokenize by using comma delimiter
              i am john
               thank you

Java StringTokenizer Example 2

This is the same example as above except that there we will use different method hasMoreElements() and nextElement() and in this example there is space between john and , e.g i am john , thank you.


import java.util.StringTokenizer;
public class StringTokenizerExample2
{
public static void main(String args[])

{

String s = "i am john , thank you";

StringTokenizer st = new StringTokenizer(s);//by using space

StringTokenizer st1 = new StringTokenizer(s, ",");//by using comma



System.out.println("tokenize by using space delimiter");


while(st.hasMoreElements())

{
System.out.println(st.nextElement());
}

System.out.println("tokenize by using comma delimiter");


while(st1.hasMoreElements())

{

System.out.println(st1.nextElement());
}
}
}

output : 
tokenize by using space delimiter
am
john
,
thank
you
tokenize by using comma delimiter
i am john
 thank you


Share:

1 comment:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate