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]) {
  RegExp pattern = (chars != null) ? new RegExp('^[$chars]+|[$chars]+\$') : new RegExp(r'^\s+|\s+$');
  return str.replaceAll(pattern, '');
}