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

被刻印的时光 ゝ 提交于 2021-02-04 16:51:07

问题


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

input:

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

output:

nums = "2030052"

回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/48014624/how-to-convert-liststring-into-string-in-dart-without-iteration

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!