Java 包含不起作用的方法

Java 包含不起作用的方法,java,Java,你能告诉我为什么在下面的代码中包含不工作的内容吗 String str= "/XYZ1/Test3-NV28"+ "/678/13855497776650XB"+ "/123/"; if ("XYZ".contains(str)) System.out.println("yes"); else System.out.println("no"); } RP变化 if ("XYZ".contains(str))

你能告诉我为什么在下面的代码中包含不工作的内容吗

String str=
        "/XYZ1/Test3-NV28"+
        "/678/13855497776650XB"+
        "/123/";

if ("XYZ".contains(str))

        System.out.println("yes");
else
    System.out.println("no");
    }
RP

变化

if ("XYZ".contains(str))

您以错误的方式使用了。你必须把它当作

if (str.contains("XYZ")) {

}
请进一步澄清

 String x= "test";
    if (x.contains("est")) {  //true
        System.out.println("true");
    }
 if(x.contains("set")){  //false
     System.out.println("false");
 }
将此
if(“XYZ”.contains(str))
更改为
if(str.contains(“XYZ”)

您正在搜索
String str
中是否存在
XYZ

当且仅当此字符串包含指定的字符值序列时,返回true。

应为

str.contains("XYZ")
因为
str
包含
“XYZ”
XYZ
不包含
str

包含方法名称本身表示方法的含义…

更改此选项

if ("XYZ".contains(str))

因为
str
包含
“XYZ”
。而
“XYZ”
不包含
str

这里是

如果我们有两个字符串,并且我们想使用contains方法比较它们,那么包含的字符串必须是原始字符串的子集。 i、 e


如果(str.contains(“XYZ”),您正在寻找另一种方法
str
包含“XYZ”,但“XYZ”不包含
str
。编辑了我的问题。不正确邹,我也试过了。如果我说str.contains(“XYZ”),它返回true。
str
确实包含
XYZ
“/XYZ1/Test3-NV28”
),所以包含返回true是正常的。您应该阅读文档:“当且仅当此字符串包含指定的字符值序列时返回true”。因此,如何解决我的问题,我只想看到该字符串中存在的特定工作。您所说的“我只想看到该字符串中存在的特定工作”是什么意思?
if ("XYZ".contains(str))
if (str.contains("XYZ"))
 String originalString="Africa is a continent";
    System.oout.println(originalString.contains("Africa"))
     ///true, will be printed because Africa is in 
                                 ///the originalString
       }
   System.out.println("Africa".contains(OriginalString))// it will print false 

    }