joinWithLastSeparator method

String joinWithLastSeparator({
  1. String separator = ', ',
  2. String lastSeparator = ' & ',
})

Joins the elements of the iterable into a single string with the given separator and lastSeparator. The lastSeparator is used to join the last two elements of the iterable.

Implementation

String joinWithLastSeparator({
  String separator = ', ',
  String lastSeparator = ' & ',
}) {
  if (isEmpty) {
    return '';
  }
  if (length == 1) {
    return first.toString();
  }
  final list = toList();
  if (length == 2) {
    return list.join(lastSeparator);
  }

  final lastTwo = list.sublist(list.length - 2).join(lastSeparator);
  final allButLastTwo = list.sublist(0, list.length - 2).join(separator);
  return '$allButLastTwo$separator$lastTwo';
}