containsIgnoreCase method
Checks if this string contains other in a case-insensitive manner.
Following standard string semantics, an empty string is considered to be
contained in any string (including empty strings). Returns false only
when other is null.
Args:
other (String?): The substringSafe to search for. Returns false if null.
Returns:
bool: true if this string contains other (case-insensitive), false if
other is null.
Example:
'Hello World'.containsIgnoreCase('world'); // true
'Hello World'.containsIgnoreCase('HELLO'); // true
'Hello World'.containsIgnoreCase('xyz'); // false
'Hello World'.containsIgnoreCase(''); // true (empty string is always contained)
'Hello World'.containsIgnoreCase(null); // false
''.containsIgnoreCase(''); // true
Implementation
bool containsIgnoreCase(String? other) {
// Null cannot be contained
if (other == null) return false;
// Empty string is always contained (standard string semantics)
if (other.isEmpty) return true;
// Perform a case-insensitive search
return toLowerCase().contains(other.toLowerCase());
}