Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3850026
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T16:56:45+00:00 2026-05-19T16:56:45+00:00

I need to go through a large list of string url’s and extract the

  • 0

I need to go through a large list of string url’s and extract the domain name from them.

For example:

http://www.stackoverflow.com/questions would extract http://www.stackoverflow.com

I originally was using new URL(theUrlString).getHost() but the URL object initialization adds a lot of time to the process and seems unneeded.

Is there a faster method to extract the host name that would be as reliable?

Thanks

Edit: My mistake, yes the www. would be included in domain name example above. Also, these urls may be http or https

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T16:56:46+00:00Added an answer on May 19, 2026 at 4:56 pm

    If you want to handle https etc, I suggest you do something like this:

    int slashslash = url.indexOf("//") + 2;
    domain = url.substring(slashslash, url.indexOf('/', slashslash));
    

    Note that this is includes the www part (just as URL.getHost() would do) which is actually part of the domain name.

    Edit Requested via comments

    Here are two methods that might be helpful:

    /**
     * Will take a url such as http://www.stackoverflow.com and return www.stackoverflow.com
     * 
     * @param url
     * @return
     */
    public static String getHost(String url){
        if(url == null || url.length() == 0)
            return "";
    
        int doubleslash = url.indexOf("//");
        if(doubleslash == -1)
            doubleslash = 0;
        else
            doubleslash += 2;
    
        int end = url.indexOf('/', doubleslash);
        end = end >= 0 ? end : url.length();
    
        int port = url.indexOf(':', doubleslash);
        end = (port > 0 && port < end) ? port : end;
    
        return url.substring(doubleslash, end);
    }
    
    
    /**  Based on : http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3.3_r1/android/webkit/CookieManager.java#CookieManager.getBaseDomain%28java.lang.String%29
     * Get the base domain for a given host or url. E.g. mail.google.com will return google.com
     * @param host 
     * @return 
     */
    public static String getBaseDomain(String url) {
        String host = getHost(url);
    
        int startIndex = 0;
        int nextIndex = host.indexOf('.');
        int lastIndex = host.lastIndexOf('.');
        while (nextIndex < lastIndex) {
            startIndex = nextIndex + 1;
            nextIndex = host.indexOf('.', startIndex);
        }
        if (startIndex > 0) {
            return host.substring(startIndex);
        } else {
            return host;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to loop through a List of Dictionaries List<Dictionary<string,string>> to populate a DataTable.
I need to parse through a string and add single quotes around each Guid
I need to parse through a CMS generated HTML page and add list item
I need to be able to store a large list of ordered items in
I need help with filtering rows from jqgrid. I have a grid with large
Right, I'm iterating through a large binary file I need to minimise the time
I need parse through a file and do some processing into it. The file
I have a huge ammount of photos that need sorting through. I need to
I need to iterate through all byte values (-128 to 127 inclusive). I could
I need to iterate through nested entities in a Twig template. Entity A ->

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.