isPalindrome method
Returns true if this string reads the same forwards and backwards (optionally ignoring case/punctuation).
Compares whole characters (grapheme clusters), so '👋a👋' is a
palindrome and an emoji or accented letter is never split.
Audited: 2026-06-12 11:26 EDT
Implementation
@useResult
bool isPalindrome({bool ignoreCase = true, bool ignorePunctuation = false}) {
// Normalize per the flags first, then compare with two cursors moving inward
// from both ends; one mismatch disproves it. The cursors meeting (i >= j)
// means every mirrored pair matched.
String normalized = this;
if (ignoreCase) normalized = normalized.toLowerCase();
if (ignorePunctuation) normalized = normalized.replaceAll(RegExp(r'[^\w]'), '');
// Graphemes, not UTF-16 code units: mirroring code units reversed each
// surrogate pair, so '👋a👋' was reported as not a palindrome.
final List<String> chars = normalized.characters.toList();
for (int i = 0, j = chars.length - 1; i < j; i++, j--) {
if (chars[i] != chars[j]) return false;
}
return true;
}