I an developing a web browser. Is there a built-in way to convert partial url strings like "example.com"
or "www.example.com"
etc... to "http://www.example.com/"
?

- 131
- 8
-
1`gmail.com` is neither an URL nor an URI. It is a domain. – Boris the Spider Sep 04 '15 at 16:57
-
Wikipedia is always nice. https://en.wikipedia.org/wiki/Uniform_resource_identifier#The_relationship_between_URIs.2C_URLs.2C_and_URNs – Jeroen Vannevel Sep 04 '15 at 16:57
-
I think you did not realize my question. I edited my post. – Sahar Avr Sep 04 '15 at 17:02
-
1Note that `www.` is not necessarily a valid subdomain (e.g. `en.wikipedia.org` works, but `www.en.wikipedia.org` doesn't). – Sizik Sep 04 '15 at 17:15
3 Answers
I don't know for certain if this will solve your issue - but I might recommend using the build in URL classes to attempt to solve this problem. Please take a look at the links below and decide if they are helpful.
https://docs.oracle.com/javase/tutorial/networking/urls/

- 1
- 1

- 948
- 12
- 28
Assuming you are trying to operate on a String
typed into the location bar of your web browser then the only thing you can do with confidence is to check whether the string already begins with "http://" or "https://" or some other scheme (such as "ftp://") and, if it does not, prepend "http://" to the start.
As has been mentioned in the comments, it is wrong to assume that the subdomain should be set to "www" because not all web servers have configured this subdomain. (And it's easy for a web server to be configured to redirect a request to the "www" subdomain if the website owner prefers.)
So I'd say the code you need is simply this:
// Create a static final class field.
static final Pattern STARTS_WITH_SCHEME = Pattern.compile("^ *[a-z]+://");
// Then within your method code make use of the Pattern.
if (!STARTS_WITH_SCHEME.matcher(urlString).find()) {
urlString = "http://" + urlString.trim();
}
This will check whether your urlString
begins with any scheme followed by a colon and double-slash. If not, then "http://" will be prepended to your urlString
. Note that the Pattern
allows spaces to appear at the very start of the urlString
and then the method code trims them away.

- 12,967
- 4
- 37
- 68
From what I've understood from your question,this is what you pretend.
String url = "www.example.com"
//Checks if the string does not start with http://
if (!url.startsWith("http://" && !url.endsWith("/") {
//Appends the http in the begginning of the String
url.insert(0, "http://");
}
//Checks if the string does not end with /
if (!url.endsWith("/") {
//Appends the slash in the end of the url
url.append("/");
}
Note that I added the verification of the url String, because in some cases I guess you have no certainty of the url format.

- 7,035
- 1
- 26
- 45
-
2This doesn't deal with all the cases... I'm sure there's a built-in way to convert all cases to a correct url – Sahar Avr Sep 04 '15 at 17:09
-
-
@Sh4d0wsPlyr thanks, I haven't noticed that part. Updated my answer. – Bruno Caceiro Sep 04 '15 at 17:11
-
You probably want to make `!url.endsWith("/")` a separate check, since the string could have one but not the other. – Sizik Sep 04 '15 at 17:11
-
@SaharAvr There is no built-in, as far as I know, so you just have to do it manually. It's just a matter of dealing with strings. – Bruno Caceiro Sep 04 '15 at 17:11
-