Java Split String - Split A String Into Tokens With Stringtokenizer
Java Split String - Split A String Into Tokens With Stringtokenizer
Java Split String - Split A String Into Tokens With Stringtokenizer
http://www.devdaily.com/java/edu/pj/pj010006
devdaily.com
Java split String - Split a String into tokens with StringTokenizer
Submitted by alvin on July 28, 2007 - 12:49am
new posts
Design Patterns in Java Win a copy of Introducing HTML5 (free) Linux processor and memory information commands Drupal websites speed and performance tuning Huge Drupal Boost module performance improvements Use sed to modify files in place How to schedule an automatic Mac wake up time How to succeed as a consultant Apache RedirectMatch wildcard examples Two Java inheritance tests / interview questions more
tags: break
java
java
parse
split
string
stringtokenizer
Java String splitting FAQ: Can you share some examples of how to split a String in Java? When working with any general-purpose programming language, it's often necessary to break a large string into smaller components. Whether you're working with Unix system files, older Windows' ".ini" files, or maybe flat files in a text database, you'll often read in a record of information, and then break that record up into smaller chunks. More recently, I've used this method to programmatically interpret the contents of HTML pages for web "robots". In this article we'll demonstrate how to split a Java String into smaller components, called tokens. We'll begin by breaking a simple well-known sentence into words, and then we'll demonstrate how to use the same technique to work with a flat-file database.
A simple snippet of code to break that sentence into individual words using the Java StringTokenizer class looks like this:
String speech = "Four score and seven years ago"; StringTokenizer st = new StringTokenizer(speech); while (st.hasMoreTokens()) { println(st.nextToken()); }
categories
android (17) best practices (54) career (43) cvs (27) design (17) drupal (38) eclipse (6) gadgets (105) git (12) intellij (4) java (396) jbuilder (20) jdbc (21) swing (74) jsp (9) latex (26) linux/unix (241) mac os x (271) mysql (47) news (106)
In this example, the variable speech is passed into the Java StringTokenizer constructor method. Because StringTokenizer is not given a field separator value as an input parameter, it uses it's default field separator, and assumes that fields within the string are separated by whitespace characters (spaces, tabs, and carriage-return characters). Therefore, each time through the while loop a word is printed on a separate line, and the resulting output from this snippet of code looks like this:
1 of 6
06/24/2011 05:25 PM
http://www.devdaily.com/java/edu/pj/pj010006
ooa/ood (10) page 1 (280) perl (152) php (85) postgresql (17) ruby (54) servlets (10) svn (7) technology (67) testing (14) uml (21)
The while loop test checks to see if there are any tokens left in the st object. As long as there are, the println statement is executed. Once there are no tokens remaining, the println statement is skipped and the while loop is exited. One quick note before continuing: If you try this example, you'll need to import the java.util.* package to run this example, like this:
import java.util.*;
Homer:Simpson:Springfield:??? Hank:Hill:Arlen:Texas
Assuming that the first record (the Homer Simpson record) was read into a String variable named dbRecord, the record could be broken up into four fields and printed as follows:
// assume 'dbRecord' is assigned the value of the first database record StringTokenizer st = new StringTokenizer(dbRecord, ":"); String fname = st.nextToken(); String lname = st.nextToken(); String city = st.nextToken(); String state = st.nextToken(); println("First Name: println("Last Name: println("City: println("State: " + fname); " + lname); " + city); " + state);
In this example, we assume that the variable dbRecord already contains the entire first record of information from our database. Because we know that the fields of each record are separated by the colon character, we specify that the colon character should be the field delimiter (or field separator) when we call the StringTokenizer constructor, like this:
2 of 6
06/24/2011 05:25 PM
http://www.devdaily.com/java/edu/pj/pj010006
Java Certification Guide www.epractizelabs.com Java Certification Training Lab Complete study material. Java Database www.versant.com High Scalability, High Availability Large Data Sets, High Performance Informix 4GL Outsourcing www.moredata.eu Meet a team with 25+ yrs experience developing 4GL & OSS applications Learn English Vocabulary LearnRealEnglish.com The Secrets To Learning Vocabulary Faster. Free Email Course.
After that, it's a simple matter to break the record into it's four fields using the nextToken() method of the StringTokenizer class. This technique is demonstrated completely in Listing 1, where the entire
customer.db text database is read (record-by-record), and printed.
// Listing 1: TokenTest.java import java.io.*; import java.util.*; class TokenTest { public static void main (String[] args) { TokenTest tt = new TokenTest(); tt.dbTest(); }
void dbTest() { DataInputStream dis = null; String dbRecord = null; try { File f = new File("customer.db"); FileInputStream fis = new FileInputStream(f); BufferedInputStream bis = new BufferedInputStream(fis); dis = new DataInputStream(bis); // read the first record of the database while ( (dbRecord = dis.readLine()) != null) { StringTokenizer st = new StringTokenizer(dbRecord, ":"); String fname = st.nextToken(); String lname = st.nextToken(); String city = st.nextToken(); String state = st.nextToken(); System.out.println("First Name: System.out.println("Last Name: System.out.println("City: System.out.println("State: } } catch (IOException e) { // catch io errors from FileInputStream or readLine() System.out.println("Uh oh, got an IOException error: " + e.getMessage()); } finally { // if the file opened okay, make sure we close it if (dis != null) { try { dis.close(); } catch (IOException ioe) { System.out.println("IOException error trying to close the file: " + e.getMessage()); " + fname); " + lname); " + city); " + state + "\n");
3 of 6
06/24/2011 05:25 PM
http://www.devdaily.com/java/edu/pj/pj010006
Listing 1 (above): The file TokenTest.java program demonstrates a method to read every record in a text database file (customer.db), and break the data records into tokens (i.e., fields) using the StringTokenizer class. Although the code shown in Listing 1 is not a good example of object-oriented programming style, it does demonstrate our technique of reading a file and breaking it's records into fields using the StringTokenizer class.
StumbleUpon
del.icio.us
Digg
related articles
Java StringTokenizer example Perl CSV file column extraction Opening and reading files with Java JDK 1.0.x Ruby CSV - An example of how to split CSV row data into fields Perl split function - how to process text data files
alvin's blog
Thanks
Submitted by Jwebuser (not verified) on November 10, 2009 - 9:58am.
4 of 6
06/24/2011 05:25 PM
http://www.devdaily.com/java/edu/pj/pj010006
I thank you for this. I've been looking for solution for my problem for days. reply
Anonymous
E-mail:
The content of this field is kept private and will not be shown publicly.
Homepage:
Subject:
Comment: *
Notify me when new comments are posted All comments Replies to my comment
Preview
5 of 6
06/24/2011 05:25 PM
http://www.devdaily.com/java/edu/pj/pj010006
java
java applets java faqs misc content java source code test projects lejos
unix
man (help) pages unix by example tutorials
misc
privacy policy terms & conditions subscribe unsubscribe wincvs tutorial function point analysis (fpa) fpa tutorial function point analysis software
other
living in alaska personal diary of selling a business mvp - alaska website design and programming codemee - java source code talkeetna, alaska
perl
perl faqs programs perl recipes perl tutorials perl source code
6 of 6
06/24/2011 05:25 PM