I have this String
.
var String a = '["one", "two", "three", "four"]';
var ab = (a.split(','));
print(ab[0]); // return ["one"
I want to convert this to List<String>
.
The problem is it returns square bracket too. I want to List looks this ["one", "two", "three", "four"]
not [["one", "two", "three", "four"]]
.
How can I convert this properly?
Strings can be converted to lists using list() .
Pass the List<String> as a parameter to the constructor of a new ArrayList<Object> . List<Object> objectList = new ArrayList<Object>(stringList);
split method Null safety allMatches, and returns the list of the substrings between the matches, before the first match, and after the last match. const string = 'Hello world! '; final splitted = string. split(' '); print(splitted); // [Hello, world!];
Your string looks like a valid JSON, so this should work for you:
import 'dart:convert';
...
var a = '["one", "two", "three", "four"]';
var ab = json.decode(a);
print(ab[0]); // returns "one"
void main(){
String listA = '["one", "two", "three", "four"]';
var a = jsonDecode(listA);
print(a[0]); // print one
String listB = 'one,two,three,four';
var b = (listB.split(','));
print(b[0]); // print one
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With