Question:
How to parse words from a comma separated string?
Answer:
Use: string.split()
It splits a string around matches of the given regular expression.
See: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split
It splits a string around matches of the given regular expression.
See: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split
Code:
public class DelimitedByCommas { public static void main(String[] args) { String str = "Paul,John,George,Ringo"; System.out.println("String: " + str); System.out.println("1st: " + str.split(",")[0] ); System.out.println("2nd: " + str.split(",")[1] ); System.out.println("3rd: " + str.split(",")[2] ); System.out.println("4th: " + str.split(",")[3] ); } }
Output:
$ java DelimitedByCommas String: Paul,John,George,Ringo 1st: Paul 2nd: John 3rd: George 4th: Ringo