splitSync static method

List<String> splitSync(
  1. File file, {
  2. required List<String> splitters,
  3. List<Object>? delimiters,
  4. bool removeSplitters = true,
  5. bool trimParts = false,
  6. Encoding encoding = utf8,
})

Synchronously reads file as a string and splits it into parts, slicing the string at each occurrence of any of the splitters. file not be null, splitters must not be null or empty.

Note: If using a linebreak (\n) as a splitter, it's a good idea to include \r\n before \n, as Windows and various internet protocols will automatically replace linebreaks with \r\n for backwards compatibility with legacy platforms. Not doing so shouldn't cause any problems in most cases, but will leave strings with a hidden \r character. \n\r is also used as a line ending by some systems.

delimiters can be provided as Strings and/or Delimiters to denote blocks of text that shouldn't be parsed for splitters.

If removeSplitters is true, each string part will be captured without the splitting character(s), if false, the splitter will be included with the part. removeSplitters must not be null.

If trimParts is true, the parser will trim the whitespace around each part when they are captured. trimParts must not be null.

Implementation

static List<String> splitSync(
  File file, {
  required List<String> splitters,
  List<Object>? delimiters,
  bool removeSplitters = true,
  bool trimParts = false,
  Encoding encoding = utf8,
}) {
  assert(splitters.isNotEmpty);
  assert(delimiters == null ||
      delimiters.every(
          (delimiter) => delimiter is String || delimiter is Delimiter));

  final string = file.readAsStringSync(encoding: encoding);

  return StringSplitterConverter(
    splitters: splitters,
    delimiters: delimiters,
    removeSplitters: removeSplitters,
    trimParts: trimParts,
  ).convert(string);
}