How to convert list<String> into String in Dart without iteration?

StringListDart

String Problem Overview


Is there a method in Dart like the String.join() method in Java & c#?

input:

nums: ["20",  "3005",  "2"]

output:

nums = "2030052"

String Solutions


Solution 1 - String

join is a method of the List class, rather than String:

List<String> yourList = ["20", "3005", "2"];

// To test that the above the above
yourList.join() == '2030052';     // true
yourList.join(',') == '20,3005,2'; // true, with "," delimiter

Solution 2 - String

This might not be the best solution, but you can reduce a collection to a single value by iteratively combining elements of the collection using the reduce method in Dart Lists.

String nums = numsList.reduce((value, element) => value + ',' + element);

You have to remember that, the iterable must have at least one element. If it has only one element, that element is returned.

Solution 3 - String

List<String> onlyString=[];
onlyString.add("Flutter");
for(int i=0; i < onlyString.length; i++){
print(onlyString[i].toString());}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHaikelView Question on Stackoverflow
Solution 1 - StringMike FahyView Answer on Stackoverflow
Solution 2 - StringRangana UdaraView Answer on Stackoverflow
Solution 3 - StringDaniel BetancourthView Answer on Stackoverflow