Android String to URL Convert getDomain(String url)

Here you can find the source of getDomain(String url)

Description

get Domain

License

Open Source License

Declaration

public static String getDomain(String url) 

Method Source Code

//package com.java2s;

public class Main {
    public static String getDomain(String url) {
        if (isEmptyOrNull(url))
            return null;
        String domain;//from   www  .j a  va2 s .c o  m
        try {
            int pos = url.indexOf("//");
            domain = url.substring(pos + 2);
            int endpos = domain.indexOf("/");

            if (url.indexOf("http") > -1) {
                domain = "http://" + domain.substring(0, endpos);
            } else {
                domain = "https://" + domain.substring(0, endpos);
            }
        } catch (StringIndexOutOfBoundsException e) {
            return null;
        } catch (Exception e) {
            return null;
        }
        return domain;
    }

    public static boolean isEmptyOrNull(String str) {
        return str == null || str.length() == 0
                || str.contentEquals("null") || str.trim().equals("");
    }

    public static String substring(String str, int srcPos,
            int specialCharsLength) {
        if (str == null || "".equals(str) || specialCharsLength < 1) {
            return "";
        }
        if (srcPos < 0) {
            srcPos = 0;
        }
        if (specialCharsLength <= 0) {
            return "";
        }
        char[] chars = str.toCharArray();
        if (srcPos > chars.length) {
            return "";
        }

        int strLength = 0;
        for (int i = 0; i < chars.length; i++) {
            strLength += getSpecialCharLength(chars[i]);
        }

        if (strLength < specialCharsLength) {
            return str;
        }
        int charsLength = getCharsLength(chars, specialCharsLength);
        return new StringBuffer()
                .append(new String(chars, srcPos, charsLength))
                .append("...").toString();
    }

    private static int getSpecialCharLength(char c) {
        if (isLetter(c)) {
            return 1;
        } else {
            return 2;
        }
    }

    private static int getCharsLength(char[] chars, int specialCharsLength) {
        int count = 0;
        int normalCharsLength = 0;
        for (int i = 0; i < chars.length; i++) {
            int specialCharLength = getSpecialCharLength(chars[i]);
            if (count <= specialCharsLength - specialCharLength) {
                count += specialCharLength;
                normalCharsLength++;
            } else {
                break;
            }
        }
        return normalCharsLength;
    }

    public static boolean isLetter(char c) {
        int k = 0x80;
        return c / k == 0 ? true : false;
    }
}

Related

  1. isUrl(String s)
  2. isUrl(String str)
  3. isUrl(String s)
  4. isUrlValid(String str)
  5. isHttpUrl(String input)
  6. findUrl(StringBuilder sb)
  7. isImageUrl(String imgUrl)
  8. getBaseUrl(String pageUrl)
  9. getYoutubeId(String url)