2

Is there any native mechanism in java/kotlin/android sdk that can help me unify all simmilar symbols?

What I need to achieve is that weather user types café, cafe or cafę to AutoCompleteTextView he should receive the same set of hints.

EDIT: Additional explanation. I need more than normalize string (café -> cafe). There are cases where my hints will include these special letters and when 'café' will be the hint to be displayed, normalizing user input will make it into 'cafe' and it won't match required hint.

Michał Powłoka
  • 1,424
  • 1
  • 13
  • 31
  • You may want to look at [this](https://stackoverflow.com/questions/3322152/is-there-a-way-to-get-rid-of-accents-and-convert-a-whole-string-to-regular-lette), although it is for desktop you can probably use the same on Android, as the package is in the [docs](https://developer.android.com/reference/java/text/Normalizer.html) – jrtapsell Feb 05 '18 at 14:32
  • I am afraid normalizer is not enough for me. If I would like for each 'cafe' and 'café' to provide me with hint 'cafe' it would be fine. But if I want to provide user with hint 'café' (if he is spanish or smth) the normalizer won't cover it. – Michał Powłoka Feb 05 '18 at 14:35
  • Do you need to keep `café` and `cafe` distinct, or can they be the same hint with an internationalised name? – jrtapsell Feb 05 '18 at 14:39
  • I can keep them distinct – Michał Powłoka Feb 05 '18 at 14:47
  • You should store the normalized form of all hints _in addition to the accented form_ and match the normalized form of input to it. You should do this anyway to be able to index by the normalized form. – Marko Topolnik Feb 05 '18 at 14:54

1 Answers1

3

There is a class in Java for these cases: java.text.Collator. You need to set the strength of comparison and use it to compare the strings. Here's an example of how you use it:

val collator = Collator.getInstance().apply {
     strength = Collator.PRIMARY
}
val order = collator.compare("café", "cafe")
Malcolm
  • 41,014
  • 11
  • 68
  • 91
  • Thanks, that is exactly what I need. I have found problem with that tho, in Polish simmilar letters (e and ę for example) are considered primary differences and I cannot compare them equal with any strength. I may prepare set of rules for languages which has the same problem, unless there is something better to do with that. – Michał Powłoka Feb 06 '18 at 08:39
  • @MichałPowłoka In that case, there is [`RuleBasedCollator`](https://developer.android.com/reference/java/text/RuleBasedCollator.html), where you can set the rules yourself. – Malcolm Feb 06 '18 at 14:01