split method

Iterable<String> split (
  1. String lines,
  2. [int start = 0,
  3. int? end]
)

Split lines into individual lines.

If start and end are provided, only split the contents of lines.substring(start, end). The start and end values must specify a valid sub-range of lines (0 <= start <= end <= lines.length).

Implementation

static Iterable<String> split(String lines, [int start = 0, int? end]) sync* {
  end = RangeError.checkValidRange(start, end, lines.length);
  // TODO(38725): Remove workaround when assignment promotion is implemented
  if (end == null) {
    throw RangeError("Invalid range");
  }
  var sliceStart = start;
  var char = 0;
  for (var i = start; i < end; i++) {
    var previousChar = char;
    char = lines.codeUnitAt(i);
    if (char != _CR) {
      if (char != _LF) continue;
      if (previousChar == _CR) {
        sliceStart = i + 1;
        continue;
      }
    }
    yield lines.substring(sliceStart, i);
    sliceStart = i + 1;
  }
  if (sliceStart < end) {
    yield lines.substring(sliceStart, end);
  }
}

© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dart.dev/stable/2.13.0/dart-convert/LineSplitter/split.html