join 方法

String join(
  1. [String separator = ""]
)
override

将每个元素转换为 String 并连接这些字符串。

遍历此可迭代元素的元素,通过调用 Object.toString 将每个元素转换为 String,然后使用 separator 字符串在元素间插入连接这些字符串。

示例

final planetsByMass = <double, String>{0.06: 'Mercury', 0.81: 'Venus',
  0.11: 'Mars'};
final joinedNames = planetsByMass.values.join('-'); // Mercury-Venus-Mars

实现

String join([String separator = ""]) {
  Iterator<E> iterator = this.iterator;
  if (!iterator.moveNext()) return "";
  var first = iterator.current.toString();
  if (!iterator.moveNext()) return first;
  var buffer = StringBuffer(first);
  // TODO(51681): Drop null check when de-supporting pre-2.12 code.
  if (separator == null || separator.isEmpty) {
    do {
      buffer.write(iterator.current);
    } while (iterator.moveNext());
  } else {
    do {
      buffer
        ..write(separator)
        ..write(iterator.current);
    } while (iterator.moveNext());
  }
  return buffer.toString();
}