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

孤街醉人 提交于 2019-12-13 04:27:07

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/14481218/how-to-avoid-different-letters-cases-%c3%a1-%c3%a9-%c3%ad-%c3%b3-%c3%ba-in-search-mode-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!