Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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,我写了一个程序,可以识别字符串中的特定单词,但我有一个问题。如何识别字符串中没有空格的单词?(在一个完整的字符串中) 如果您不需要实际找到确切的字符串,只需将regex部分替换为以下部分: if(string.matches(".*" + word + ".*")){ System.out.println("Found"); } 这应该行得通 正如其他人有效地指出的,matches函数只是告诉字符串是否匹配正则表达式。如果需要检查str1是否包含str2,则应使用contains()函

我写了一个程序,可以识别字符串中的特定单词,但我有一个问题。如何识别字符串中没有空格的单词?(在一个完整的字符串中)


如果您不需要实际找到确切的字符串,只需将regex部分替换为以下部分:

if(string.matches(".*" + word + ".*")){
    System.out.println("Found");
}
这应该行得通


正如其他人有效地指出的,matches函数只是告诉字符串是否匹配正则表达式。如果需要检查str1是否包含str2,则应使用contains()函数。

您的问题不清楚。请给出输入和预期输出的示例。为什么不使用
String.contains(“yourword”)
if(string.matches(".*" + word + ".*")){
    System.out.println("Found");
}