首页 > 代码库 > sell- 获取邮箱的后缀
sell- 获取邮箱的后缀
1.
public static void main(String[] args) { System.out.println(getEmailSuffix("jim_chen28270@163.com")); // 163.com System.out.println(getEmailSuffix("@qq.com")); //qq.com } private static String getEmailSuffix(String email) { email = defaultIfBlank(email,"").trim(); if (!email.isEmpty()) { int index = email.indexOf(‘@‘) + 1; if (index < email.length()) { return email.substring(index).toLowerCase(); } } return ""; } public static <T extends CharSequence> T defaultIfBlank(final T str, final T defaultStr) { return isBlank(str) ? defaultStr : str; } public static boolean isBlank(final CharSequence cs) { int strLen; if (cs == null || (strLen = cs.length()) == 0) { return true; } for (int i = 0; i < strLen; i++) { if (!Character.isWhitespace(cs.charAt(i))) { return false; } } return true; } }
2. 优化,封装到一个公用的字符串操作类中去
public static String getEmailSuffix(String email) { email = MyStringUtils.defaultIfBlank(email,"").trim(); if (!email.isEmpty()) { int index = email.indexOf(‘@‘) + 1; if (index < email.length()) { return email.substring(index).toLowerCase(); } } return ""; } //自己封装的字符串操作类 class MyStringUtils { public static <T extends CharSequence> T defaultIfBlank(final T str, final T defaultStr) { return isBlank(str) ? defaultStr : str; } public static boolean isBlank(final CharSequence cs) { int strLen; if (cs == null || (strLen = cs.length()) == 0) { return true; } for (int i = 0; i < strLen; i++) { if (!Character.isWhitespace(cs.charAt(i))) { return false; } } return true; } public static boolean isNotBlank(final CharSequence cs) { return !isBlank(cs); } public static String defaultString(Object obj) { return obj == null ? "" : obj.toString(); } }
sell- 获取邮箱的后缀
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。