Split String in flutter
1,714
The reason you are getting 2 is because it is the first position (0) of the string variable date
.
When you split a string, it will return a list/array.
String date = "2020-10-07";
final dateList = date.split("-");
print("split " + dateList[0]);
//expected "split 2020"
Comments
-
Tony over 1 year
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 ?
-
Andrew Cheong over 3 yearsYour
date
is still a String. Yo should assign the return value of::split
to an array var. -
Tony over 3 years@AndrewCheong I actually reading this tutorialkart.com/dart/dart-split-string. The String will become
list
after usingsplit
, no? -
Andrew Cheong over 3 yearsNope, in that reference you also see it returning a list of strings.
-
Xenolion over 3 yearsYou are still using the same date object it is
immutable
useprint( date.split('-')[0]);
-