toAlphaOnly method

String toAlphaOnly({
  1. bool allowSpace = false,
})

Removes all characters that are not letters (A-Z, a-z).

Implementation

String toAlphaOnly({bool allowSpace = false}) {
  // Define the regex based on whether spaces are allowed.
  final RegExp regExp = allowSpace ? RegExp('[^A-Za-z ]') : RegExp('[^A-Za-z]');
  // Replace all non-matching characters.
  return replaceAll(regExp, '');
}