gpt4 book ai didi

java - 检查字符串数组是否包含值

转载 作者:行者123 更新时间:2023-12-02 10:54:12 25 4
gpt4 key购买 nike

我必须检查字符串数组 test2[] 是否包含 test1[] 值。怎么做?两个数组的大小不同。我还必须检查 test2[] 是否包含 test1[] 的子字符串。

String[] test = {"Test1", "Test2"};
String[] test2 = {"Test3", "Test4", "Test5", "Test6", "Test1 - Test7"};

最佳答案

您只需要几个嵌套循环并进行迭代。您的问题不明确,您所说的“包含”一词可能意味着“等于”或“包含子字符串”。无论哪种情况,如果您想要相等匹配,只需将 .contains() 替换为 .equals() 即可。

for (String value : test) {
for (String sampleString : test2) {
if (sampleString.contains(value)) {
System.out.println("Value " + value + " is contained in the array in " + sampleString);
}
}
}

关于java - 检查字符串数组是否包含值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51911104/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com