Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * Copyright (C) 2010 Peter Karich <jetwick_@_pannous_._info>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    public static final String HTTP = "http://";
    public static final String HTTPS = "https://";

    public static String extractDomain(String url) {
        // url shorteners seems to have a "domain.de" shorter or equal to 11
        // the longest was tinyurl.com the shortest is t.co
        if (url.startsWith(HTTP))
            url = url.substring(HTTP.length());
        if (url.startsWith(HTTPS))
            url = url.substring(HTTPS.length());

        int index = url.indexOf("/");
        if (index < 0)
            index = Math.max(url.length(), url.indexOf(" "));

        String domain = url.substring(0, index);
        if (domain.startsWith("www."))
            domain = domain.substring(4);

        // skip if the domain of domain.de is of zero length or if the "de" is less then 2 chars
        index = domain.indexOf(".");
        if (index < 0 || domain.length() < 4)
            return "";

        return domain;
    }
}