Here you can find the source of getDomain(String url)
public static String getDomain(String url)
//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; } }