CodeMonkey CodeMonkey - 4 years ago 152
Java Question

Splitting an input String

I've the following Query String,

QUERY 0 2016-08-25 18:32 2016-08-26 18:31

I want to split it into 5 string objects such that
2016-08-25 18:32
2016-08-26 18:31
are parsed as one object.

String[] arr = str.split(" ");

Splits the String into 7 strings splitting the dates. I'm not sure how to do it using regular expressions using Java.
Any help appreciated.

Answer Source

as long as the positions of the items are static, simply manipulate your output array after the split:

String sdate = arr[3] + ' ' + arr[4];
String edate = arr[5] + ' ' + arr[6];

if needed, make a new array:

String[] arr2 = {arr[0], arr[1], arr[2], sdate, edate};

good luck!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download