在java中从2个数组中比较和提取相似的字符串,双字字符串不比较,.split(",")返回空白。

问题描述 投票:0回答:1

我知道以前也有人问过类似的问题,我昨天也问过这个问题,然而我还是找不到问题的答案!我的问题是:我的问题是比较数组和提取HashSet的相似性。

我正在用HashSet比较数组并提取相似性。

final String[] abc = nlp_text.split(",");
final String[] xyz = getResources().getStringArray(R.array.xyz);

final HashSet<String> set = new HashSet<>();
for(String a : abc) set.add(a.toLowerCase());
final HashSet<String> set1 = new HashSet<>();
for(String a : xyz) set1.add(a.toLowerCase());
set.retainAll(set1);
String[] pqr= {};
pqr = set.toArray(pqr);

这将返回空白! 虽然有相似的字符串用逗号隔开!nlp_text是数组中的名字列表,例如:"Nataly Portman,Johnny Depp,John Doe,Jane Doe,Natasha,Sasha,...等等"。数组xyz也是名字(数据库中的名字,所以大约300个)。

如果用("" ")拆分,对单字字符串有效,但对双字字符串无效。例如,字符串 "John "和 "Doe "将被分别识别,但 "John Doe "却不能。因此,尝试了逗号、"-"、".",甚至双空格! 什么都没有!有什么想法吗?

有什么想法吗?先谢谢你!!!

java arrays string split
1个回答
0
投票

我的假设是你有空格.你的nlp_text可能看起来是这样的:

"Nataly Portman, Johnny Depp, John Doe, Jane Doe, Natasha, Sasha"

而你的数组也是这样的:

{ "Johnny Depp", "John Doe", "Jane Doe", "Natasha" }

如果是的话,在拆分之后,你的数组中的一些元素会有空格。final String[] abc.

如果是这样,添加 trim() 之后 toLowerCase():

a.toLowerCase().trim()

© www.soinside.com 2019 - 2024. All rights reserved.