Reputation: 39
I have the followings string from the DB "[["22-1-2017;10:00-19:00"],["22-1-2017;10:00-19:00"]]"
Is there an easy way to transform the string into an arrayList?
Thanks in advance
Upvotes: 0
Views: 90
Reputation: 1361
I got one solution for your problem, if you want the date in an String Array:
String s = "[[\"22-1-2017;10:00-19:00\"],[\"22-1-2017;10:00-19:00\"]]";
// first remove outter brackets
s = s.substring(1, s.length());
s = s.substring(0, s.length()-1);
// Create a Pattern for getting everything within brackets
Pattern r = Pattern.compile("\\[(.*?)\\]");
// Now create matcher object.
Matcher m = r.matcher(s);
List<String> l = new ArrayList<>();
while (m.find()) {
l.add(m.group(1));
}
System.out.println(l);
If you want it without the quotations use
Pattern r = Pattern.compile("\\[\"(.*?)\"\\]");
Hope this helps
Edit: If you use the
Pattern r = Pattern.compile("\\[\"(.*?)\"\\]");
you do not need to remove the outer brackets at all, as it already looks for content within:
Upvotes: 0
Reputation: 1275
Try this,
String a = "[[\"22-1-2017;10:00-19:00\"],[\"22-1-2017;10:00-19:00\"]]";
String[] b = a.replace("[", "").replace("]", "").replace("\"", "").split(",");
List<String> c = Arrays.asList(b);
Output = [22-1-2017;10:00-19:00, 22-1-2017;10:00-19:00]
Upvotes: 0
Reputation: 124295
How about using JSON parser like gson?
String jsonArray = "[[\"22-1-2017;10:00-19:00\"],[\"22-1-2017;10:00-19:00\"]]";
Type listType = new TypeToken<List<List<String>>>(){}.getType();
List<List<String>> list = new Gson().fromJson(jsonArray, listType);
System.out.println(list);
Output: [[22-1-2017;10:00-19:00], [22-1-2017;10:00-19:00]]
Upvotes: 1