Parsing a Comma-Separated String

String str = "tim,kerry,timmy,camden";
											String[] results = str.split(",");

The split() method on the String class accepts a regular expression as its only parameter, and will return an array of String objects split according to the rules of the passed-in regular expression. This makes parsing a comma-separated string an easy task. In this phrase, we simply pass a comma into the split() method, and we get back an array of strings containing the comma-separated data. So the results array in our phrase would contain the following content:

results[0] = tim
results[1] = kerry
results[2] = timmy
results[3] = camden

Another useful class for taking apart strings is the StringTokenizer class. We will repeat the phrase using ...

Get Java™ Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.