trim function

String trim(
  1. String str
)

Removes all whitespace before and after a string. Unlike JavaScript's trim function, it converts two or more spaces between sentences into a single space.

Implementation

String trim(String str) {
  if (str.isEmpty) {
    return '';
  }

  return str.trim().replaceAll(RegExp(r'\s{2,}'), ' ');
}