zoukankan      html  css  js  c++  java
  • 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();
        }
    }
    
  • 相关阅读:
    队列学哪个好
    python web 开发
    随笔
    问题集录
    大早晨地,快睡觉了,才想明白多线程代理验证是如何做的
    线程真的挺不错的,但是多了的时候也有点让人头痛
    愁死我了,写个控制台怎么好象在写解释器一样
    我越发地发现,我是在写一个解释器了
    哈哈,真有意思
    我要崩溃了。。。。
  • 原文地址:https://www.cnblogs.com/bravolove/p/6116934.html
Copyright © 2011-2022 走看看