trim function

String trim(
  1. String str, [
  2. String? chars
])

trim characters (whitespace by default) from both sides of the input

Implementation

String trim(String str, [String? chars]) {
  final pattern =
      (chars != null) ? RegExp('^[$chars]+|[$chars]+\$') : RegExp(r'^\s+|\s+$');
  return str.replaceAll(pattern, '');
}