How to avoid different letters cases (á, é, í, ó, ú) in search mode - Android

StackOverflow https://stackoverflow.com/questions/14481218

  •  17-01-2022
  •  | 
  •  

سؤال

I'm building an app that should replace the default contacts phone book, and I want to add a search bar for search a contact's name.

For now I have all the contacts in my own DB and a TextWatcher for keep tracking any change in the editText - then builds a query with the char sequence that the user entered. This is works fine, I'm getting all the contacts the user search like all the contacts starts with "a". Now I want to add a new functionality to ignore special letters like: á, é, í, ó, ú.

I want that the user could type a and still get all the contacts that starts with á the same for e and all the other letters.

Is there a way to do this in the query itself or there is something that Java/Android give us that accomplish that.

Thanks.

هل كانت مفيدة؟

المحلول

You can use java.text.Normalizer to strip accents.

String strippedAccent = Normalizer.normalize(someText, Normalizer.Form.NFD).replaceAll("\\p{InCombiningDiacriticalMarks}+", "");

f you want to use it in a query, you should probably add a field into your DB containing the stripped-down version of the searchable text.

نصائح أخرى

This change worked that quite well for nubdial. It's reasonably fast, too.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top