0

I have a java applet text area that I paste strings separated by a comma.

How can I explode a list separated by a comma, as in PHP, and store each string separated by a comma into an array called name[]?

Example list: Sara,Michael,Sam,Katie,Kyle,Tom,Dan

String name[] = {"Sara", "Michael", "Sam", "Katie", "Kyle", "Tom", "Dan"};

Thank you.

3 Answers 3

8
String[] name = str.split(',');
Sign up to request clarification or add additional context in comments.

Comments

2

It's pretty basic stuff. Java String's have a built in string-splitting method.

String[] name = rawText.split(',');

Comments

1

Pretty simple using String.split(), as others mentioned. Might be a good idea to trim() the results to be safe (e.g. to handle input of Sara, Michael, Sam, Katie, Kyle, Tom, Dan)

String[] names = input.split(',');
for (int i=0; i<names.length; i++) {
    names[i] = names[i].trim();
}

1 Comment

There are simpler ways to do the trimming ... assuming that it is part of the requirement; e.g. String[] names = input.trim().split("\\s*,\\s*");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.