How to Check if a String is a URL
·
211 words
·
1 minute read
Here’s a little snippet to determine if a string is well structured and valid url. This can be useful for pre-empting if a http call will work - or preventing failures from even occurring. In this snippet we’re using a function to tidy this logic and make it reusable and we are essentially parsing the url and checking for any errors.
ParseRequestURI()
is our primary basic check, but it will allow strings like ‘Test: Test’ to pass (which we don’t want). We therefore combine it with a url parser to check that both a scheme (like http) and a host exist.
|
|