removeSpecialCharacters static method

String removeSpecialCharacters(
  1. String text, {
  2. bool keepSpaces = true,
})

Remove special characters from string

text - The text to process keepSpaces - Whether to keep spaces (default: true) Returns text without special characters

Implementation

static String removeSpecialCharacters(String text, {bool keepSpaces = true}) {
  if (keepSpaces) {
    return text.replaceAll(RegExp(r'[^a-zA-Z0-9\s]'), '');
  } else {
    return text.replaceAll(RegExp(r'[^a-zA-Z0-9]'), '');
  }
}