比方說,我有一本書的標題,我在數據庫中搜索它。數據庫產生匹配,其中一些是完全匹配的,其中一些是部分匹配的。如何檢查一個字符串中的每個單詞是否在另一個字符串中找到?
A full match
是當搜索結果中的每個單詞由搜索項中的單詞表示時。(i.e. there does not have to be a complete overlap on both sides)
我只關心找到完整匹配。
所以,如果我爲"Ernest Hemingway - The Old Man and the Sea"
鍵入搜索,並將結果返回如下:
Charles Nordhoff - Men Against The Sea
Rodman Philbrick - The Young Man and the Sea
Ernest Hemingway - The Old Man and the Sea
Ernest Hemingway - The Sun Also Rises
Ernest Hemingway - A Farewell to Arms
Ernest Hemingway - For Whom the Bell Tolls
Ernest Hemingway - A Moveable Feast
Ernest Hemingway - True at First Light
Men Against The Sea
The Old Man and the Sea
The Old Man and the Sea Dog
有在此列表中有兩個full matches
:(根據上述定義)
Ernest Hemingway - The Old Man and the Sea
The Old Man and the Sea
在Java中這樣做,假設我有兩個變量:
String searchTerms;
List<String> searchResults;
在例如searchTerms
上述代表着什麼我輸入:Ernest Hemingway - The Old Man and the Sea
searchResults
代表字符串我從上面的數據庫返回的名單。
for (String result : searchResults) {
// How to check for a full match?
// (each word in `result` is found in `searchTerms`
}
我的問題是:在這個for-loop
,我如何檢查在result
字符串的每一個字是否有在searchTerms
字符串對應詞?