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
}