how to remove the delimiters from a string using StringTokenizer class in collections in java
Previous | Home | Next |
package r4r.co.in;
import java.util.*;
public class CollectionsExample {
/**
* @param args
*/
public static void main(String[] args) throws NullPointerException, NoSuchElementException
{
// TODO Auto-generated method stub
try
{
String string = "12 + 12 = 24"+" 35 / 7 = 5"; // defined a string
System.out.println("The original string is: "+string);
StringTokenizer st = new StringTokenizer(string, "+ = /"); // passed the string to the StringTokenizer constructor with + = / as delimiters
System.out.println("The tokens in the string are: ");
while(st.hasMoreTokens()) // loop till the st has more tokens to offer
System.out.println(st.nextToken()); // displaying the tokens
}
catch(NullPointerException e)
{
e.printStackTrace();
}
catch(NoSuchElementException e)
{
e.printStackTrace();
}
}
}
The output of the above example is:
The original string is: 12 + 12 = 24 35 / 7 = 5
The tokens in the string are:
12
12
24
35
7
5
Previous | Home | Next |