Roubie
Roubie

Reputation: 299

Separate a list of input in string

I have this kind of input in string david=michael,sarah,tina,justin

David is the father and michael,sarah,tina and justin are his children. I want to make an array named michael and inside the array are his children. How can I do that in Java? Do I need to use StringTokenizer?

Upvotes: 0

Views: 177

Answers (3)

WhiteFang34
WhiteFang34

Reputation: 72039

Here's one approach, assuming that your input strings are always formatted the same way:

String input = "david=michael,sarah,tina,justin";
String father = input.split("=")[0];
String[] children = input.split("=")[1].split(",");

Note that if there's no = in your input you'll get an exception getting the children.

Upvotes: 1

MeBigFatGuy
MeBigFatGuy

Reputation: 28568

String[] names = input.split("=|,");

names[0] is the parent, names[1] ... names[names.length - 1] are the kids.

Upvotes: 0

Joseph Ottinger
Joseph Ottinger

Reputation: 4951

Use Properties to split the keys into reasonable groupings, then String.split() to pull out the children of the parent key.

Upvotes: 0

Related Questions