I have a query like this "abc=1, def=2, ghi=3"
and I need to parse it into arrays like this keys = [abc, def, ghi] and values = [1,2,3]
currently my code is like this
String[] terms = query.split(",");
int termsCount = terms.length;
String[] keys = new String[termsCount];
String[] values = new Object[termsCount];
for(int i=0; i<termsCount; i++)
{
if(terms[i].contains("="))
{
keys[i] = terms[i].split("=")[0];
values[i] = terms[i].split("=")[1];
}
}
sometimes, the query might be empty or may not be well formed - I need to take care of that scenarios too.
Am I doing this right? Is there a better way to do this?
String[] values = new Object[termsCount];
should beString[] values = new String[termsCount];
. Alternatively, you could chuck the whole arrays thing and do what the posters below suggest. \$\endgroup\$