Free CoreJava Programs Source codes | Examples | Video Tutorials Download

Monday, February 16, 2009

StringTokenizer Program


/**

* Write a example for StringTokenizer.

*


*

*/

public class StringTokenizer {

public void stringTokenizerDemo() {

// source string

String strSource = "Ritesh Loves India";

// delimeter string

String strDeli = "i";

System.out.println("\nSource String is : " + strSource);

System.out.println("\nExample 1:-");

System.out.println("------------");

System.out.println("\tDelimeter String is : " + strDeli);

System.out.println("\tTokens Are :");

// specify the string with delimeter string by which it would be splited

java.util.StringTokenizer st = new java.util.StringTokenizer(strSource, strDeli);

while (st.hasMoreTokens()) {

System.out.println("\t\t" + st.nextToken());

}

// another example

strDeli = " ";

System.out.println("Example 2:-");

System.out.println("------------");

System.out.println("\tDelimeter String is : " + strDeli + "");

System.out.println("\tTokens Are : ");

st = new java.util.StringTokenizer(strSource, strDeli);

while (st.hasMoreTokens())

System.out.println("\t\t" + st.nextToken());

}

/**

* execution begin from here

*

* @param args

* command line argument

*/

public static void main(String[] args) {

new StringTokenizer().stringTokenizerDemo();

}

}

No comments:

Post a Comment

Followers