© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
Quick Links
Easy Studies

String Splitting Using Java


This program describes the way to Split a String using Regular expression and StringTokenizer. It has the String to which we are splitting and the Splitpoint -". This is the Point where the string is to be splitted. This program splits the given String into tokens based on the character presented on the String. The character based on which, the String is to be splitted is known as Split point. StringTokenizer gives another way for splitting a String into tokens. Based on the delimiter, String is splitted. hasMoreTokens () method is to check whether it has more tokens. nextToken () method is to get next token from the tokenizer.

String Splitting Using Java :

import java.util.*; public class SplitString { public static void main(String[] args) { String text = "For some college -Presidents want law-revisited" + " saying age limit-has not curbed-that problems on campuses"; System.out.println("STRING BEFORE SPLITTING IS:"); System.out.println(text); new SplitString().split(text); } public void split(String Text) { String token[] = null; String Splitpoint = "-"; token = Text.split(Splitpoint); System.out.println("SPLITTED STRING IS:"); for (int i = 0; i < token.length; i++) { System.out.println(token[i]); } System.out.println(); System.out.println("Using StringTokenizer"); StringTokenizer str=new StringTokenizer(Text,"-"); while(str.hasMoreTokens()){ String tokens=str.nextToken(); System.out.println(tokens); } } }

Sample ScreenShot:

SLogix Student Projects

⇓Student Projects⇓
⇑Student Projects⇑