1

I'm getting the following String from an API:

"[4:00 PM - 5:00 PM, 5:00 PM - 6:00 PM, 6:00 PM - 7:00 PM, 7:00 PM - 8:00 PM, 10:00 AM - 11:00 AM, 11:00 AM - 12:00 PM, 12:00 PM - 1:00 PM, 1:00 PM - 2:00 PM, 2:00 PM - 3:00 PM]"

I want to convert this String to a List. How to convert it?

4 Answers 4

2

Code :

void main() {
  String mylist = "[4:00 PM - 5:00 PM, 5:00 PM - 6:00 PM, 6:00 PM - 7:00 PM, 7:00 PM - 8:00 PM, 10:00 AM - 11:00 AM, 11:00 AM - 12:00 PM, 12:00 PM - 1:00 PM, 1:00 PM - 2:00 PM, 2:00 PM - 3:00 PM]";
  
  mylist = mylist.replaceAll('[', '');
  mylist = mylist.replaceAll(']', '');
  List<String> newList = mylist.split(',');
  print(newList[0]);
}

Output :

4:00 PM - 5:00 PM
Sign up to request clarification or add additional context in comments.

Comments

2
void main() {
  String list = "[4:00 PM - 5:00 PM, 5:00 PM - 6:00 PM,"
      " 6:00 PM - 7:00 PM, 7:00 PM - 8:00 PM,"
      " 10:00 AM - 11:00 AM, 11:00 AM - 12:00 PM,"
      " 12:00 PM - 1:00 PM, 1:00 PM - 2:00 PM, 2:00 PM - 3:00 PM]";

  List<String> out = [];

  String batch = "";
  for (String s in list.split("")) {
    if (s == "[" || s == "]") continue;
    if (s == ",") {
      out.add(batch);
      batch = "";
    } else
      batch += s;
  }

  print(out);
}



Output -> [4:00 PM - 5:00 PM,  5:00 PM - 6:00 PM,  6:00 PM - 7:00 PM,  7:00 PM - 8:00 PM,  10:00 AM - 11:00 AM,  11:00 AM - 12:00 PM,  12:00 PM - 1:00 PM,  1:00 PM - 2:00 PM]

Hope this helps! 😋

Comments

2

this works

String data =
      "[4:00 PM - 5:00 PM, 5:00 PM - 6:00 PM, 6:00 PM - 7:00 PM, 7:00 PM - 8:00 PM, 10:00 AM - 11:00 AM, 11:00 AM - 12:00 PM, 12:00 PM - 1:00 PM, 1:00 PM - 2:00 PM, 2:00 PM - 3:00 PM]";

  List<String> dataList = data.replaceAll('[', '').replaceAll(']', '').split(',');

Comments

0

You can use split() method in Dart. It will look like this:

 void main() {

   String times = "4:00 PM - 5:00 PM, 5:00 PM - 6:00 PM, 6:00 PM - 7:00 PM, 7:00 PM - 8:00 PM, 10:00 AM - 11:00 AM, 11:00 AM - 12:00 PM, 12:00 PM - 1:00 PM, 1:00 PM - 2:00 PM, 2:00 PM - 3:00 PM"; 

   // Splitting the string 
   // across comma 
   print(times.split(",")); 

   List<String> timesList = times.split(",");
 }


 // Output: 
 //[4:00 PM - 5:00 PM,  5:00 PM - 6:00 PM,  6:00 PM - 7:00 PM,  7:00 PM - 8:00 PM,  10:00 AM - 11:00 AM,  11:00 AM - 12:00 PM,  12:00 PM - 1:00 PM,  1:00 PM - 2:00 PM,  2:00 PM - 3:00 PM]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.