Pigasus
Pigasus

Reputation: 130

Ignore font type when comparing strings

I have a foreign language dictionary that I load into a map which users will enter terms for translation against; it's every basic. However, I am running into an issue with the map look (map.get("term")) up because of the font types of both the user and of the dictionary (which is a flat text file); they are different and about 1/3 of the terms aren't found because of the difference of font. For example (fonts don't show up so I included the uri encode value to show difference..URI encoding is not a solution I'm looking for):

String a1 = "A";// uri encode value \uff21
String a2 = "A";// uri encode value \u0041
Boolean a3 = a1.equals(a2);
System.out.println(a3); // false

The dictionary file is quite large and I don't have the luxury of data scrubbing before loading it into the map. I haven't been able to find any string function or API that will handle this sort of issue. Has anyone ran into this before and can anyone offer some ideas on how to compare strings while ignoring fonts?

Upvotes: 0

Views: 478

Answers (1)

D.R.
D.R.

Reputation: 21224

You can use Java's Collator class:

Collator c = Collator.getInstance(Locale.US);
c.setStrength(Collator.IDENTICAL);
c.setDecomposition(Collator.FULL_DECOMPOSITION);
boolen a3 = c.equals(a1, a2); // should be true now

See https://docs.oracle.com/javase/7/docs/api/java/text/Collator.html for more information.

Upvotes: 4

Related Questions