StringTokenizer In Java

Chapter: String Handling Last Updated: 22-07-2016 07:44:02 UTC

Program:

            /* ............... START ............... */
                
import java.util.StringTokenizer;

public class JavaStringTokenizer {
	public static void main(String[] args) {

		// default delimiter (space)
		String str1 = "Welcome To JavaScan.com";
		StringTokenizer st1 = new StringTokenizer(str1);

		System.out.println("String 1:");
		while (st1.hasMoreElements()) {
			System.out.println("  " + st1.nextElement());
		}

		// use comma for delimiter
		String str2 = "Java,Scan,Sun,Java,Example";
		StringTokenizer st2 = new StringTokenizer(str2, ",");

		System.out.println("String 2:");
		while (st2.hasMoreElements()) {
			System.out.println("  " + st2.nextElement());
		}

		// use colon for delimiter
		String str3 = "Java : String : Tocken:scan";
		StringTokenizer st3 = new StringTokenizer(str3, ":");

		System.out.println("String 3:");
		while (st3.hasMoreElements()) {
			System.out.println("  " + st3.nextElement());
		}
	}
}
                /* ............... END ............... */
        

Output

String 1:
  Welcome
  To
  JavaScan.com
String 2:
  Java
  Scan
  Sun
  Java
  Example
String 3:
  Java 
   String 
   Tocken
  scan

Notes:

  • The java.util.StringTokenizer class allows you to break a string into tokens.
  • StringTokenizer(String str) - constructor a string tokenizer for the specified string.
  • StringTokenizer(String str, String delim) - creates StringTokenizer with specified string and delimeter.
  • StringTokenizer(String str, String delim, boolean returnValue) - creates StringTokenizer with specified string, delimeter and returnValue. If return value is true, delimiter characters are considered to be tokens. If it is false, delimiter characters serve to separate tokens.

Tags

StringTokenizer, Java, String

Similar Programs Chapter Last Updated
Java StringBuffer CharAt String Handling 27-05-2017
Java StringBuilder EnsureCapacity String Handling 27-05-2017
Java StringBuffer Capacity Example String Handling 27-05-2017
Java Character At Particular Index String Handling 16-05-2017
Java Convert String To Array Example String Handling 16-05-2017
Java String Trim Method String Handling 15-05-2017
Java String Character Case Check String Handling 15-05-2017
Java Find All Substrings Of String String Handling 14-05-2017
Java String Conversion Using ValueOf String Handling 30-03-2017
Immutable String In Java String Handling 24-03-2017
Java Float To String Using toString() String Handling 05-11-2016
Java Program To Convert Integer To String Using toString() String Handling 05-11-2016
Java Reverse String Using Stack String Handling 24-10-2016
Java Integer To String Conversion String Handling 21-10-2016
Java Program To Check Isomorphic Strings String Handling 19-10-2016
Java String Unicode Code Point Example String Handling 28-08-2016
Concatenate Strings In Java Example String Handling 21-08-2016
String Optimization In Java String Handling 21-08-2016
Java String Split Example String Handling 20-08-2016
Java String Search String Handling 20-08-2016
Java String Replace String Handling 20-08-2016
Java String Remove Character Example String Handling 20-08-2016
Java String Last Occurance String Handling 20-08-2016
Java String Compare Example String Handling 20-08-2016
String Initialization In Java Example String Handling 20-06-2016
StringBuffer SubString In Java String Handling 09-05-2016
StringBuffer Reverse In Java String Handling 09-05-2016
StringBuffer Insert In Java String Handling 09-05-2016
StringBuffer Append In Java String Handling 09-05-2016
Java String isEmpty() String Handling 01-05-2016

1 2 3