Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if a string contains accents

How to know if a string contains accents?

like image 669
Mercer Avatar asked May 05 '10 14:05

Mercer


People also ask

What letters can have accents?

The most common accents are the acute (é), grave (è), circumflex (â, î or ô), tilde (ñ), umlaut and dieresis (ü or ï – the same symbol is used for two different purposes), and cedilla (ç). Accent marks (also referred to as diacritics or diacriticals) usually appear above a character.

How do I check a character in a string?

The best method to check the character in a String is the indexOf() method. It will return the index of the character present in the String, while contains() method only returns a boolean value indicating the presence or absence of the specified characters.

How do you check if a string is a single letter?

You can use string. indexOf('a') . If the char a is present in string : it returns the the index of the first occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.


2 Answers

I think the best thing you can do is using a normalizer that splits unicode characters with accents into two separate character. Java includes this in class Normalizer, see here.

This, for example, will split

U+00C1    LATIN CAPITAL LETTER A WITH ACUTE

into

U+0041    LATIN CAPITAL LETTER A
U+0301    COMBINING ACUTE ACCENT

and will do this for every character that has accents or other diacritical mark (http://en.wikipedia.org/wiki/Diacritic).

Then you can check if the resulting CharSequence has some of the accents character (and this will imply hard coding them) or simply check if the normalized version is equal to the starting one, this will imply that there isn't any character that has been decomposed. Java Normalizer already has this facility in isNormalized(CharSequence src, Normalizer.Form form), but you should check the various forms available to see if there's one suitable for your needs.

EDIT: if you just need basic accent supports (like just è é à ò ì ù) you can just go with oedo option, if you need full support for all the existing accents it would be crazy to hard code them all..

like image 88
Jack Avatar answered Sep 24 '22 13:09

Jack


if (Pattern.matches(".*[éèàù].*", input)) {
  ....
}

add whatever accents you want to that list

like image 35
chris Avatar answered Sep 24 '22 13:09

chris