Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何检查一个字符串是否包含另一个字符串的一部分?(不是整个字符串)_Java_String - Fatal编程技术网

Java 如何检查一个字符串是否包含另一个字符串的一部分?(不是整个字符串)

Java 如何检查一个字符串是否包含另一个字符串的一部分?(不是整个字符串),java,string,Java,String,我有两个字符串: 约翰有两个苹果 2)科迪在约翰的地下室玩xbox 现在这两个字符串有“John”的共同点 但似乎没有程序化的方法来检查这一点。我能得到的最接近的方法是如何检查字符串是否包含特定单词:str1.toLowerCase().contains(str2.toLowerCase()) 那么我如何检查字符串1)是否包含字符串2的一部分呢?这可能会起作用 public static void main(String[] args) { String x = "John has 2

我有两个字符串:

约翰有两个苹果

2)科迪在约翰的地下室玩xbox

现在这两个字符串有“
John
”的共同点

但似乎没有程序化的方法来检查这一点。我能得到的最接近的方法是如何检查字符串是否包含特定单词:
str1.toLowerCase().contains(str2.toLowerCase())

那么我如何检查字符串1)是否包含字符串2的一部分呢?

这可能会起作用

public static void main(String[] args) {
    String x = "John has 2 apples.";
    String y = "Cody plays xbox in John's basement.";
    // print words of x that matches any of y
    findMatch(Arrays.asList(x.split(" ")), y);
    // print words of y that matches any of x
    findMatch(Arrays.asList(y.split(" ")), x);

}

private static void findMatch(List<String> firstArr,String statement) {
    for (String string : firstArr) {
        if(statement.contains(string)) {
            System.out.println(string);
        }
    }
}
publicstaticvoidmain(字符串[]args){
String x=“约翰有两个苹果。”;
String y=“科迪在约翰的地下室玩xbox。”;
//打印与任何y匹配的x字
findMatch(Arrays.asList(x.split(“”),y);
//打印与任何x匹配的y字
findMatch(Arrays.asList(y.split(“”),x);
}
私有静态void findMatch(List firstArr,String语句){
for(字符串:firstArr){
if(语句.包含(字符串)){
System.out.println(字符串);
}
}
}

如果您谈论的是两个字符串,则必须创建每个字符串相对于彼此的矩阵,即

1) “
abcd

2) “
cdef

矩阵)

然后检查在上述示例中是否出现双精度模式(即,
cc
dd

这意味着,对于另一个字符串的每个字符,您必须遍历每个字符串,因此我相信这会给您O(n^2)时间复杂性。对于每个对角线匹配,这将是一个匹配的标记(可能有多个)。正如@lexicore所说,这与最长的公共子串不同

如果它们是非常大的字符串,您可能不希望遍历每个字符,而是将它们标记化(即,通过将它们按空格分割),并为每个字符(或哈希表或其他内容)创建一个排序列表,这样您就可以在O(logn)的时间内写入每个字符。我认为这会给你一些类似于O((logn)^2)的东西,但至少比O(n^2)好。

基于:

私有静态列表交互(字符串s1、字符串s2){
散列集h1,h2;
h1=toHashSet(s1);
h2=toHashSet(s2);
h1.保留(h2);
返回h1.stream().collect(toList());
}
私有静态HashSet到HashSet(字符串s){
返回Arrays.stream(s.split(“[\\s@&.?$+-]+”).collect(Collectors.toCollection(HashSet::new));
}
公共静态void main(字符串[]args){
干涉(“约翰有两个苹果。”,“科迪在约翰的地下室玩xbox。”).forEach(System.out::println);
}

您需要更具体地说,注意这两个字符串也包含一个
,或者一个
“o”
“p”
,等等。我很确定,如果没有o(n^2)复杂度的东西,就没有有效的方法可以做到这一点。正如您可以想象的那样,您可能需要将这两个字符串组成一个矩阵(逐字符)并检查对角线中的双字母。编辑没关系,@Pablo说可以在O(m+n)中完成。@user9598926你必须明确。你在寻找最长的公共子字符串吗?@1N5818 O(m+n)可能是用于另一项任务。关于这一点,我认为尝试会有很大帮助。请注意,按相反的顺序:在
x
中搜索
y
中的任何单词都不会起作用,因为
“John的”
不包含在
“John有两个苹果”。
op澄清了他想要的是“如何检查字符串1是否包含字符串2的一部分?”@davidxxit就是一个例子。一个代码应该在多个“正常”情况下工作,否则它是硬编码的,这是没有意义的。那么为什么不直接输出“John”?两个字符串都可以拆分成单词并检查字符串2是否包含字符串1中的单词?谢谢@davidxxx的解释,我已经编辑了这两种情况下的代码,如果有其他方法可以这样做,你能将其添加到这里的答案中吗?
ac bc cc dc
ad bd cd dd
ae be ce de
af bc cf df
private static List<String> interection(String s1, String s2) {
    HashSet<String> h1, h2;
    h1 = toHashSet(s1);
    h2 = toHashSet(s2);
    h1.retainAll(h2);
    return h1.stream().collect(toList());
}

private static HashSet<String> toHashSet(String s) {
    return Arrays.stream(s.split("[\\s@&.'?$+-]+")).collect(Collectors.toCollection(HashSet::new));
}

public static void main (String [] args) {
    interection("John has 2 apples.", "Cody plays xbox in John's basement.").forEach(System.out::println);
}