-1

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()?

5 Answers5

0
  1. 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);
}
  1. 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://.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
itwasntme
  • 1,442
  • 4
  • 21
  • 28