Thursday, April 18, 2013

Using String split to process delimited text:


Processing delimited text is one of the most widely encountered task by a programmer.
java.lang.String class provides a very useful split() method to parse delimited strings. Lets take a look at it.
Consider a comma delimited text containing the employee details firstname, lastname, department, city, state, zip.

Lets parse the comma separated text
john, doe, d1, true, city1, state1, 1234

For the split method we pass the delimiter as ",\\s*" regular expression. This will delimit on any comma followed by zero or more white spaces.
We can process this text using java String split as:
















The Employee class is:


Lets run it with a main method:

The output of running this code is:
Employee name after split is john doe

Hope this post usefule. Please leave your comments.

Check out my other blogs:
http://www.techusergit.blogspot.com/
http://www.techusercamel.blogspot.com

No comments:

Post a Comment