在字符串数组中寻找相似的字符串 [英] Looking for similar strings in a string array
本文介绍了在字符串数组中寻找相似的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串数组.例如:
I have a string array. For example:
["Tartrazine","Orange GGN", "Riboflavin-5-Phosphate"]
我有一个字符串.例如:
And I have a string. For example:
"Riboflvin"
我想在数组中查找最相似的字符串,如果存在则获取它.所以我需要这个输出:
I want to look for most similar string in the array and get it if it exists. So I need this output:
"Riboflavin-5-Phosphate"
但是如果数组看起来像这样:
But if the array looks like this:
["Tartrazine","Orange GGN", "Quinoline"]
我想要这样的输出:
"No similar strings found"
我尝试使用 FuzzyWuzzy库,但是它显示了很多错误警报.
I tried using FuzzyWuzzy library, but it shows a lot of false alarms.
推荐答案
You can use String#contains
method, sequentially reducing the length of the string to search if the full string is not found:
String[] arr = {"Tartrazine", "Orange GGN", "Riboflavin-5-Phosphate"};
String element = "Riboflvin";
boolean found = false;
for (int i = 0; i < element.length(); i++) {
// take shorter substring if nothing found at previous step
String part = element.substring(0, element.length() - i);
// if any string from array contains this substring
if (Arrays.stream(arr).anyMatch(str -> str.contains(part))) {
System.out.println("Found part: " + part);
// then print these strings one by one
Arrays.stream(arr).filter(str -> str.contains(part))
.forEach(System.out::println);
found = true;
break;
}
}
// if nothing found
if (!found) {
System.out.println("No similar strings found");
}
输出:
Found part: Ribofl
Riboflavin-5-Phosphate
这篇关于在字符串数组中寻找相似的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文