78

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

input:

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

output:

nums = "2030052"

4 Answers 4

176

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
3
  • 2
    yourList.join()?
    – zerkms
    Dec 28, 2017 at 21:50
  • @zerkms yes this do the job and return a String Mar 23, 2020 at 18:44
  • 2
    @MohamedDernoun it was a comment to point out to the typo Mike made which they fixed.
    – zerkms
    Mar 24, 2020 at 0:01
7

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.

2

In my case I had to convert into flat string. So this is the way I found;

List<String> tags = ["#heyy", "@newday", "#newpp"];

    
print(tags.join(' ')); // "#heyy @newday #newpp"
0
List<String> onlyString=[];
onlyString.add("Flutter");
for(int i=0; i < onlyString.length; i++){
print(onlyString[i].toString());}
1
  • 7
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. Jul 9, 2021 at 8:27

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.