removeIndent method

String removeIndent(
  1. String indentation
)

Removes indentation from the start of every line if present.

Only removes the indentation string if it's actually at the start of a line; lines without the matching indentation are left unchanged.

Example:

'  line1\n  line2'.removeIndent('  ')      // 'line1\nline2'
'  line1\nline2'.removeIndent('  ')        // 'line1\nline2' (line2 unchanged)
'    line'.removeIndent('  ')              // '  line' (one indent removed)

Implementation

String removeIndent(String indentation) {
  if (isEmpty || indentation.isEmpty) return this;
  final lines = split('\n');
  return lines.map((line) {
    if (line.startsWith(indentation)) {
      return line.substring(indentation.length);
    }
    return line;
  }).join('\n');
}