简体   繁体   中英

Flutter - Is there any way to convert a string to list and a list<String> back to a string?

I have names of some persons which are given by the user so when I want to save the list of users as a string so that I can store it in SQLite DB and also share it through dynamic links can it be like the names separated by commas or full stops etc which can be again converted to a list?

You can use ' split ' to split a String into List. And then use ' join ' to join the List back to string. Please see the code below where I use | as a separator.

 String str = "user1|user2|user3|user4|user5";
 List<String> strLst = str.split("|");
 print(strLst); //[user1, user2, user3, user4, user5]
 str = strLst.join("|"); 
 print(str); //user1|user2|user3|user4|user5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM