开发者

Separate a list of input in string

开发者 https://www.devze.com 2023-03-01 04:09 出处:网络
I have this kind of input in string david=michael,sarah,tina,justin David is the father and michael,sarah,tina and jus开发者_StackOverflow社区tin are his children. I want to make an array named micha

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

David is the father and michael,sarah,tina and jus开发者_StackOverflow社区tin 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?


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.


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


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消