Im trying to split URLs, for example https://stackoverflow.com/questions/
and take only stackoverflow.com
. How can I do this in Java without using the built in function getHost()
?
Asked
Active
Viewed 1,842 times
-1

user11453590
- 37
- 7
-
1See https://stackoverflow.com/questions/4826061/what-is-the-fastest-way-to-get-the-domain-host-name-from-a-url – Bert Maurau Oct 31 '19 at 23:46
-
1Why not use the tools of the language? Why reinvent a URL parser when you already have one, that has been fully tested, and is known to work? – Andreas Oct 31 '19 at 23:55
-
`String hostName = url.split("/{1,}")[1];` – DevilsHnd - 退職した Nov 01 '19 at 00:43
5 Answers
0
- One thing you can do is use String#replaceAll. I know it's not what you want but off the bat it's a decent way to do it.
String uri = "https://stackoverflow.com/questions/";
if (uri.contains("https://")) {
uri = uri.replaceAll("https://", "");
}
if (uri.contains("http://")) {
uri = uri.replaceAll("http://", "");
}
int indexOfBackwardsSlash = uri.indexOf("/");
if (indexOfBackwardsSlash != -1) {
uri = uri.substring(0, indexOfBackwardsSlash);
}
- Use URI#getPath.
URI uri = URI.create("https...");
String path = uri.getPath();

Jason
- 5,154
- 2
- 12
- 22
-
The getPath() returns the path after the host - you means getHost more likely ? – Abs Nov 01 '19 at 00:53
-
Actually getPath returns the rawPath, which is; "The string returned by this method is equal to that returned by the getRawPath method except that all sequences of escaped octets are decoded.". – Jason Nov 01 '19 at 01:04
0
If you can put your URL into a String , there is this option :
public static void main(String []args){
String str ="https://stackoverflow.com/questions/";
String[] parts = str.split("/");
String part1 = parts[0]; //https:
String part2 = parts[1]; //'nothing'
String part3 = parts[2]; //stackoverflow.com
String part4 = parts[3]; //questions
}

zerbene
- 1,249
- 2
- 7
- 21
0
You could also use regular expressions:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class UrlRegex {
public static void main(String []args){
String url = "https://stackoverflow.com/questions/";
Pattern pat = Pattern.compile("//([^/]*)"); //match everything between "//" and "/"
Matcher m = pat.matcher(url);
if (m.find()) {
String hostname = m.group(1);
}
}
}

Eltay
- 11
- 2
0
Here you go :
Pattern pattern = Pattern.compile("^(?:(?:http)s?://)?(?<hostGroup>[^/:]+).*");
Matcher matcher = pattern.matcher("https://stackoverflow.com/questions/");
if (matcher.matches()) {
System.out.println(matcher.group("hostGroup"));
} else {
System.out.println("Not found! Invalid URL ^^");
}
The above will find stackoverflow.com for the following urls strings :
- https://stackoverflow.com/questions/
- http://stackoverflow.com/questions/
- stackoverflow.com/questions/
- stackoverflow.com:80/questions/
- stackoverflow.com/
- stackoverflow.com
I guess that's for practicing regex ? Otherwise, prefer using the standard APIs whenever possible - (in your case URI#getHost()
!)
Cheers!

Abs
- 300
- 1
- 10
0
If you are sure that you are getting the proper URL format than you can just substring it preferred places.
public static void main(String[] args) {
String url = "https://stackoverflow.com/questions/";
System.out.println(getHostFast(url));
}
public static String getHostFast(String url) {
String subbed = url.substring(url.indexOf('/') + 2);
return subbed.substring(0, subbed.indexOf('/'));
}
The error prof method would need to contain additional check (for example if the next '/' exists after dropping http://
.