OneCompiler

[Java] How to convert comma separated string into an array?

I want to convert a comma separated string into an array, how to do that in Java?

1 Answer

4 years ago by

Using .split() on a string we can convert that into an array. Following code shows how to do that.

import java.util.Arrays;

public class JavaArrays {
	public static void main(String[] args) {

		String inp = "red,green,blue";
		String[] colors = inp.split(",");

		System.out.println(Arrays.toString(colors));

	}
}

Output:

[red, green, blue]

Try online here: https://onecompiler.com/java/3xmt4bxtd

4 years ago by Karthik Divi