normaliseWhitespace method

String normaliseWhitespace()

Normalises whitespace by collapsing multiple spaces, tabs, and newlines into single spaces, then trimming.

Useful for cleaning up user input or formatting text that's been copied from formatted documents.

Example:

'  Hello   World  '.normaliseWhitespace()  // 'Hello World'
'a\n\nb'.normaliseWhitespace()             // 'a b'
'  spaced  \t  out  '.normaliseWhitespace() // 'spaced out'

Implementation

String normaliseWhitespace() {
  return replaceAll(RegExp(r'\s+'), ' ').trim();
}