How can I check if a string is a URL in Java?
Using java.net.url url class to validate a URL. The idea is to create a URL object from the specified String representation. If we do not get exception while creating the object, we return true. Else we return false.
HTMLInputElement. checkValidity() method is used to check if a string in <input> element's value attribute is URL . The checkvalidity() method returns true if the value is a proper URL and false if the input is not a proper URL.
For a URI to be valid, you only have to verify it against this syntax. Connecting to it verifies that a server is registered for and listening to that URI which 1. is an entirely separate condition from the validity of the URI itself, 2.
To check Valid URL string you just have to use Uri. parse() like below. This also returns true for string "http:". There is also Uri.
You can try to create a java.net.URL
object out of it. If it is not a proper URL, a MalformedURLException
will be thrown.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With