Eric Oudin Eric Oudin - 1 year ago 91
Java Question

Boolean not returning as expected in String.Contains()?

I have this sample from my project and I need to know why the result is what it is.

public class Main
public static void main(String[] args)
//url:, websiteList:, URL Contains Returns bool: false
String url = "";
String contains = "";
System.out.println("Returns bool: " + url.contains(contains));



Returns bool: false

Answer Source

Code is always doing what you ask it to do:

 String url = "


 String contains = "";

https versus http!

So the real answer is: especially when you are a beginner, chances that your code uncovered "some Java bug" is relatively small (very close to zero in reality!)

There is a much higher chance that your assumptions are wrong. You either don't fully understand the methods you are calling, or there is a subtle flaw in your input data.

Finally: also work on your naming. contains isn't a very good name here; you better call it expectedUrl, or something alike!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download