Storing strings into an array?
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 ea开发者_运维百科ch 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.
String[] name = str.split(',');
It's pretty basic stuff. Java String
's have a built in string-splitting method.
String[] name = rawText.split(',');
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();
}
精彩评论