Realm case insensitive search syntax
Asked Answered
D

2

9

I get filtered objects like:

realm.objects(Post.self).filter("title contains '\(searchText)'")

But I need case insensitive search option, Realm docs say:

Case-insensitive comparisons for strings, such as name CONTAINS[c] ‘Ja’. Note that only characters “A-Z” and “a-z” will be ignored for case. Can be combined with the [d] modifier.

So how do I need do?

realm.objects(Post.self).filter("title contains[c] '\(searchText)'")

doesn't work...

UPD:

Got it. I was trying filter cyrillic symbols. So next question, where can I add cyrillic filtering?

Dieback answered 2/5, 2017 at 18:48 Comment(1)
how did you resolve your issue?Janelljanella
B
21

Please use NSPredicate's interpolation rather than String interpolation:

realm.objects(Post.self).filter("title contains[c] %@", searchText)
Bootless answered 2/5, 2017 at 20:16 Comment(1)
"where can I add cyrillic filtering?" Try performing a query with both "case-insensitive" and "diacritic-insensitive" modifiers: title contains[cd] %@Bootless
T
1

Solution: Swift 5.1

    realm.objects(Post.self).where { object in 
        object.title.contains(searchText.lowercased(), options: .caseInsensitive)
    }
Transcontinental answered 26/2, 2024 at 20:28 Comment(1)
Actually, there is no point in converting the searchText to lowercased String. The comparison will be case-insensitive anyways. Also, if you want case and diacritic-insensitive search, write options: [.caseInsensitive, .diacriticInsensitive]Vizier

© 2022 - 2025 — McMap. All rights reserved.