removeSpecialCharacters function

String removeSpecialCharacters(
  1. String inputString
)

Removes special characters from a string, leaving only alphanumeric characters, spaces, hyphens, underscores, and certain accented characters

Implementation

String removeSpecialCharacters(String inputString) {
  if (inputString.isEmpty) {
    throw ArgumentError("input_string must be a non-empty string");
  }

  // Pattern to keep only allowed characters
  final pattern = RegExp(r'[^a-zA-Z0-9\s\-\_áéíóúñÁÉÍÓÚÑüÜ]');
  final cleanedString = inputString.replaceAll(pattern, ' ');

  // Remove extra spaces and return
  return cleanedString
      .split(RegExp(r'\s+'))
      .where((s) => s.isNotEmpty)
      .join(' ');
}