Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

contains with collator

Tags:

java

contains

I have to test whether a string is included in another one but without considering case or accents (French accents in this case).

For example the function must return true if I search for "rhone" in the string "Vallée du Rhône".

The Collator is useful for string comparison with accents but does not provide a contains function.

Is there an easy way to do the job ? A regex maybe ?

Additional information :
I just need a true / false return value, I don't care about number of matches or position of the test string in the reference string.

like image 716
YCI Avatar asked Jan 05 '12 16:01

YCI


1 Answers

You can use Normalizer to reduce strings to stripped-down versions that you can compare directly.

Edit: to be clear

String normalized = Normalizer.normalize(text, Normalizer.Form.NFD);
String ascii = normalized.replaceAll("[^\\p{ASCII}]", "");
like image 108
Viruzzo Avatar answered Sep 29 '22 00:09

Viruzzo