I using split method to split the String.
String date = "2020-10-07";
date.split("-");
print("split " + date[0]);
I expect will get 2020, but why it return 2 ?
It is 2 because there no change has been done to the variable date( the same without the split), but with split you can access the list like this below
String date = "2020-10-07";
var first = date.split("-").first;//returns the first element of the list
print("split " + first);
(Any String).split(Pattern) returns List<String>.
Since no operation was done to change the variable (it anyhow can't store List<String>).
You are left with:
date.split("-").first); (I guess you are going to return the first split)Also data[0] -> is an operation on a string hence you are getting 0th position -> 2
dateis still a String. Yo should assign the return value of::splitto an array var.listafter usingsplit, no?immutableuseprint( date.split('-')[0]);