isPalindrome property

bool get isPalindrome

Checks if the string is a palindrome.

Implementation

bool get isPalindrome {
  if (isEmptyOrNull) return false;

  final cleanString = this!
      .toLowerCase()
      .replaceAll(RegExp(r'\s+'), '')
      .replaceAll(RegExp('[^0-9a-zA-Z]+'), '');

  // Iterate only up to half the length of the string
  for (var i = 0; i < cleanString.length ~/ 2; i++) {
    if (cleanString[i] != cleanString[cleanString.length - i - 1]) {
      return false;
    }
  }
  return true;
}