-2

this like a string array {hi, hi, hello}, but if I have a string that is shaped like that, can i do this by entering a string variable into the string array ?

like this:
String animal = "kangaroo, rabbit, dog, cat, elephant";
String[] array = { animal };

the result is:
array[0] : kangaroo
array[1] : rabbit

3

1 Answer 1

1
   String animal = "kangaroo, rabbit, dog, cat, elephant";
   String[] array = animal.split(", ");
   System.out.println(Arrays.toString(array));
Sign up to request clarification or add additional context in comments.

1 Comment

Use \\s*,\\s* instead of ", " because there may or may not be space(s) after comma.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.