Java: Does anyone have method to find best match of string in array?

java, matching, pattern-matching, string-matching

Solution

Depends on how you define "closest" but one common way is by using a Levenshtein Distance score. Apache Commons has such a method in StringUtils.

From there your search method basically becomes: find the string in the collection which has the smallest Levenshtein distance for a given input.

Problem

Basically I'm just trying to find a way to find the closest match (not necessarily exact) of a `String` For example, find `"delicous"` in `{"pie", "delicious", "test"}` This is pretty obvious, but the values in the array might not always be that distinct. Could someone please help me with a way to achieve this.

Original source