Splitsville: Using the String.split Method

Developers sometimes need to split a String into substrings using a delimiter such as a comma, tab, or whitespace. As we discussed in the previous section, Java 1.4 added the Pattern class for performing text comparisons with regular expressions. In addition to pattern matching, a Pattern object can split a String into an array of substrings, using the regular expression as the delimiter. For example, a one-line comma-delimited list can be split into an array of Strings, using a comma as the pattern:

 String data = "Australia,Fiji,New Zealand,Papua New Guinea"; Pattern comma = Pattern.compile(","); String[] ...

Get Wicked Cool Java 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.