我有要求比較用戶將搜索的產品名稱與可用產品。我有存儲在MySQL數據庫中的產品名稱。我正在收集所有名稱,並在我的Java服務啓動時將其獲取到應用程序級別(Java)。Java中的字符串比較,我應該使用哪種算法?
現在我的字符串比較的情況是這樣的:
Available product names:
1) Samsung galaxy s2
2) Samsung galaxy s3
3) Samsung galaxy s4
User input1: galaxy s3 - Then in this scenario my 2nd result should come first as it has 2 matching keywords 'galaxy' and 's3', where other 2 has only 1 matching keyword 'galaxy'.
User input2: s3 - Then here only 2nd result should come, because the other 2 has no matching key word.
User input3: samsung - Then here all three results should come.
任何人都可以請建議的算法將是適宜的在Java中?另外還有一件事,把所有產品名稱都帶到MySQL的應用程序級別(java)是正確的方法嗎?或者我也可以在MySQL級別執行它? (PS:我不想在MySQL端使用類似的查詢,因爲它會很慢)
hava您探索過的字符串javadoc?我看到('String.contains()')[http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#contains(java.lang.CharSequence)]很漂亮得心應手 – sanbhat
@sanbhat:String.contains()不會給我我正在尋找的結果的排名。 – Arry
非常感謝@EnginePai。 Lucene似乎適合我的需要。 – Arry