Search code examples
javastringsubstringpartial

Java find partially matching string from list


So Im creating an optical text reader using google camera api for OCR. When reaeding from the camera i want to filter the read strings by matching them to a list of strings and if the read string partially matches, the item from the list gets added and displayed.

Everything but the filtering works.

Say i have a list containing the string:

"BigSizeScrew   45**"

I want to match the read string:

"BigSizeScrew"

...to the one with the number and then add the string containing the number too.

The number and stars are for another reason but must be kept, the reason is irrelevant to the question.

tl;dr:

How do i match partially match a string to an existing string from a list?

I have the following but it exclusively searches for a 100% matching string, not if the checked string contains a substring of the read string.

for(int i =0;i<items.size();++i){
    if (list.contains(items.valueAt(i))) {
        TextBlock item = items.valueAt(i);
        stringBuilder.append(item.getValue());
        stringBuilder.append("\n");
    }
}

EDIT:

The list contains approx 200 different strings that should be matchable partially. But all in the same way; Partially matching the main string but not the numbers/stars.


Solution

  • You can use the startsWith method of String. If ocrString has value BigSizeScrew 45** then execute a for loop to the list with the strings to match, and if(ocrString.startsWith(list.get(i)) then ....