• 字符串工具类


    StringUtil 字符串工具类 

    package com.util;
    
    import java.io.UnsupportedEncodingException;
    import java.net.URLEncoder;
    import java.security.MessageDigest;
    import java.util.ArrayList;
    import java.util.Locale;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    /**
     * 字符串工具类
     */
    public class StringUtil {
    
        private StringUtil() {
            throw new AssertionError();
        }
    
        /**
         * <pre>
         * isBlank(null) = true;
         * isBlank("") = true;
         * isBlank("  ") = true;
         * isBlank("a") = false;
         * isBlank("a ") = false;
         * isBlank(" a") = false;
         * isBlank("a b") = false;
         * </pre>
         *
         * @param str 字符串
         * @return 如果字符串为空或者长度为0,返回true,否则返回false
         */
        public static boolean isBlank(String str) {
            return (str == null || str.trim().length() == 0);
        }
    
        /**
         * <pre>
         * isEmpty(null) = true;
         * isEmpty("") = true;
         * isEmpty("  ") = false;
         * </pre>
         *
         * @param c 字符序列
         * @return 如果字符序列为空或者长度为0,返回true,否则返回false
         */
        public static boolean isEmpty(CharSequence c) {
            return (c == null || c.length() == 0);
        }
    
        /**
         * 判断给定的字符串是否为null或者是空的
         * 
         * @param string 给定的字符串
         */
        public static boolean isEmpty(String string) {
            return string == null || "".equals(string.trim());
        }
    
        /**
         * 判断给定的字符串是否不为null且不为空
         * 
         * @param string 给定的字符串
         */
        public static boolean isNotEmpty(String string) {
            return !isEmpty(string);
        }
    
        /**
         * 判断给定的字符串数组中的所有字符串是否都为null或者是空的
         * 
         * @param strings 给定的字符串
         */
        public static boolean isEmpty(String... strings) {
            boolean result = true;
            for (String string : strings) {
                if (isNotEmpty(string)) {
                    result = false;
                    break;
                }
            }
            return result;
        }
    
        /**
         * 判断给定的字符串数组中是否全部都不为null且不为空
         *
         * @param strings 给定的字符串数组
         * @return 是否全部都不为null且不为空
         */
        public static boolean isNotEmpty(String... strings) {
            boolean result = true;
            for (String string : strings) {
                if (isEmpty(string)) {
                    result = false;
                    break;
                }
            }
            return result;
        }
    
        /**
         * 是否全是数字
         */
        public static boolean isAllDigital(char[] chars) {
            boolean result = true;
            for (int w = 0; w < chars.length; w++) {
                if (!Character.isDigit(chars[w])) {
                    result = false;
                    break;
                }
            }
            return result;
        }
    
        /**
         * 判断浮点数整数 int 类型
         *
         * @param str
         * @return
         */
        public static boolean isInteger(String str) {
            if (null == str || "".equals(str)) {
                return false;
            }
            Pattern pattern = Pattern.compile("^[-\+]?[\d]*$");
            return pattern.matcher(str).matches();
        }
    
        /**
         * 判断浮点数(double和float)
         */
        public static boolean isDouble(String str) {
            if (null == str || "".equals(str)) {
                return false;
            }
            Pattern pattern = Pattern.compile("^[-\+]?[.\d]*$");
            return pattern.matcher(str).matches();
        }
    
        /**
         * 获取字符序列的长度
         * 
         * <pre>
         * length(null) = 0;
         * length("") = 0;
         * length("abc") = 3;
         * </pre>
         *
         * @param c 字符序列
         * @return 如果字符序列为空,返回0,否则返回字符序列的长度
         */
        public static int length(CharSequence c) {
            return c == null ? 0 : c.length();
        }
    
        /**
         * null Object to empty string 空对象转化成空字符串
         * 
         * <pre>
         * nullStrToEmpty(null) = "";
         * nullStrToEmpty("") = "";
         * nullStrToEmpty("aa") = "aa";
         * </pre>
         *
         * @param object 对象
         * @return String
         */
        public static String nullStrToEmpty(Object object) {
            return object == null ? "" : (object instanceof String ? (String) object : object.toString());
        }
    
        /**
         * 第一个字母大写
         * 
         * @param str str
         * @return String
         */
        public static String capitalizeFirstLetter(String str) {
            if (isEmpty(str)) {
                return str;
            }
            char c = str.charAt(0);
            return (!Character.isLetter(c) || Character.isUpperCase(c)) ? str
                    : new StringBuilder(str.length()).append(Character.toUpperCase(c)).append(str.substring(1)).toString();
        }
    
        /**
         * 用utf-8编码
         * 
         * @param str 字符串
         * @return 返回一个utf8的字符串
         */
        public static String utf8Encode(String str) {
            if (!isEmpty(str) || str.getBytes().length != str.length()) {
                try {
                    return URLEncoder.encode(str, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    throw new RuntimeException("UnsupportedEncodingException occurred. ", e);
                }
            }
            return str;
        }
    
        /**
         * @param href 字符串
         * @return 返回一个html
         */
        public static String getHrefInnerHtml(String href) {
            if (isEmpty(href)) {
                return "";
            }
            String hrefReg = ".*<[\s]*a[\s]*.*>(.+?)<[\s]*/a[\s]*>.*";
            Pattern hrefPattern = Pattern.compile(hrefReg, Pattern.CASE_INSENSITIVE);
            Matcher hrefMatcher = hrefPattern.matcher(href);
            if (hrefMatcher.matches()) {
                return hrefMatcher.group(1);
            }
            return href;
        }
    
        /**
         * @param source 字符串
         * @return 返回htmL到字符串
         */
        public static String htmlEscapeCharsToString(String source) {
            return StringUtil.isEmpty(source) ? source
                    : source.replaceAll("<", "<").replaceAll(">", ">").replaceAll("&", "&");
    //                .replaceAll(""", """); 
        }
    
        /**
         * @param s 字符串
         * @return String
         */
        public static String fullWidthToHalfWidth(String s) {
            if (isEmpty(s)) {
                return s;
            }
            char[] source = s.toCharArray();
            for (int i = 0; i < source.length; i++) {
                if (source[i] == 12288) {
                    source[i] = ' ';
                    // } else if (source[i] == 12290) {
                    // source[i] = '.';
                } else if (source[i] >= 65281 && source[i] <= 65374) {
                    source[i] = (char) (source[i] - 65248);
                } else {
                    source[i] = source[i];
                }
            }
            return new String(source);
        }
    
        /**
         * @param s 字符串
         * @return 返回的数值
         */
        public static String halfWidthToFullWidth(String s) {
    
            if (isEmpty(s)) {
                return s;
            }
    
            char[] source = s.toCharArray();
            for (int i = 0; i < source.length; i++) {
                if (source[i] == ' ') {
                    source[i] = (char) 12288;
                    // } else if (source[i] == '.') {
                    // source[i] = (char)12290;
                } else if (source[i] >= 33 && source[i] <= 126) {
                    source[i] = (char) (source[i] + 65248);
                } else {
                    source[i] = source[i];
                }
            }
            return new String(source);
        }
    
        /**
         * 如果字符串是null或者空就返回""
         */
        public static String filterEmpty(String string) {
            return StringUtil.isNotEmpty(string) ? string : "";
        }
    
        /**
         * 对给定的字符串进行空白过滤
         *
         * @param string 给定的字符串
         * @return 如果给定的字符串是一个空白字符串,那么返回null;否则返回本身。
         */
        public static String filterBlank(String string) {
            if ("".equals(string)) {
                return null;
            } else {
                return string;
            }
        }
    
        /**
         * 将给定字符串的首字母转为小写
         *
         * @param str 给定字符串
         * @return 新的字符串
         */
        public static String firstLetterToLowerCase(String str) {
            return toLowerCase(str, 0, 1);
        }
    
        /**
         * 将给定字符串的首字母转为大写
         *
         * @param str 给定字符串
         * @return 新的字符串
         */
        public static String firstLetterToUpperCase(String str) {
            return toUpperCase(str, 0, 1);
        }
    
        /**
         * 判断给定的字符串是否以一个特定的字符串开头,忽略大小写
         *
         * @param sourceString 给定的字符串
         * @param newString    一个特定的字符串
         */
        public static boolean startsWithIgnoreCase(String sourceString, String newString) {
            int newLength = newString.length();
            int sourceLength = sourceString.length();
            if (newLength == sourceLength) {
                return newString.equalsIgnoreCase(sourceString);
            } else if (newLength < sourceLength) {
                char[] newChars = new char[newLength];
                sourceString.getChars(0, newLength, newChars, 0);
                return newString.equalsIgnoreCase(String.valueOf(newChars));
            } else {
                return false;
            }
        }
    
        /**
         * 判断给定的字符串是否以一个特定的字符串结尾,忽略大小写
         *
         * @param sourceString 给定的字符串
         * @param newString    一个特定的字符串
         */
        public static boolean endsWithIgnoreCase(String sourceString, String newString) {
            int newLength = newString.length();
            int sourceLength = sourceString.length();
            if (newLength == sourceLength) {
                return newString.equalsIgnoreCase(sourceString);
            } else if (newLength < sourceLength) {
                char[] newChars = new char[newLength];
                sourceString.getChars(sourceLength - newLength, sourceLength, newChars, 0);
                return newString.equalsIgnoreCase(String.valueOf(newChars));
            } else {
                return false;
            }
        }
    
        /**
         * 检查字符串长度,如果字符串的长度超过maxLength,就截取前maxLength个字符串并在末尾拼上appendString
         */
        public static String checkLength(String string, int maxLength, String appendString) {
            if (string.length() > maxLength) {
                string = string.substring(0, maxLength);
                if (appendString != null) {
                    string += appendString;
                }
            }
            return string;
        }
    
        /**
         * 检查字符串长度,如果字符串的长度超过maxLength,就截取前maxLength个字符串并在末尾拼上…
         */
        public static String checkLength(String string, int maxLength) {
            return checkLength(string, maxLength, "…");
        }
    
        /**
         * 判断输入的字符串是否全部为大写
         *
         * @param str
         * @return
         */
        public static boolean checkAllUpperCase(String str) {
            for (int i = 0; i < str.length(); i++) {
                char c = str.charAt(i);
                if (c >= 97 && c <= 122) {
                    return false;
                }
            }
            return true;
        }
    
        /**
         * 将给定字符串中给定的区域的字符转换成小写
         *
         * @param str        给定字符串中
         * @param beginIndex 开始索引(包括)
         * @param endIndex   结束索引(不包括)
         * @return 新的字符串
         */
        public static String toLowerCase(String str, int beginIndex, int endIndex) {
            return str.replaceFirst(str.substring(beginIndex, endIndex),
                    str.substring(beginIndex, endIndex).toLowerCase(Locale.getDefault()));
        }
    
        /**
         * 将给定字符串中给定的区域的字符转换成大写
         *
         * @param str        给定字符串中
         * @param beginIndex 开始索引(包括)
         * @param endIndex   结束索引(不包括)
         * @return 新的字符串
         */
        public static String toUpperCase(String str, int beginIndex, int endIndex) {
            return str.replaceFirst(str.substring(beginIndex, endIndex),
                    str.substring(beginIndex, endIndex).toUpperCase(Locale.getDefault()));
        }
    
        /**
         * 在给定的字符串中,用新的字符替换所有旧的字符
         * 
         * @param string  给定的字符串
         * @param oldchar 旧的字符
         * @param newchar 新的字符
         * @return 替换后的字符串
         */
        public static String replace(String string, char oldchar, char newchar) {
            char chars[] = string.toCharArray();
            for (int w = 0; w < chars.length; w++) {
                if (chars[w] == oldchar) {
                    chars[w] = newchar;
                    break;
                }
            }
            return new String(chars);
        }
    
        /**
         * 替换路径中的空格为%20
         *
         * @param str
         * @return
         */
        public static String replaceSpace(StringBuffer str) {
            for (int k = 0; k < str.length(); k++) {
                char index = str.charAt(k);
                if (index == ' ') {
                    str.replace(k, k + 1, "%20");
                }
            }
    
            return str.toString();
        }
    
        /**
         * @param str 资源
         * @return 特殊字符串切换
         */
        public static String replaceBlanktihuan(String str) {
    
            String dest = "";
            if (str != null) {
                Pattern p = Pattern.compile("\s*|	|
    |
    ");
                Matcher m = p.matcher(str);
                dest = m.replaceAll("");
            }
            return dest;
        }
    
        /**
         * 如果字符串包含%和_则需要转义\%和\_--Mysql查询包含百分号的时候用
         *
         * @param s
         * @return
         */
        public static String replaceMySQLKeyWords(String s) {
            String result = "";
            if (null != s && !"".equals(s)) {
                result = s.replaceAll("%", "\\%").replaceAll("_", "\\_");
            }
            return result;
        }
    
        /**
         * 把给定的字符串用给定的字符分割
         * 
         * @param string 给定的字符串
         * @param ch     给定的字符
         * @return 分割后的字符串数组
         */
        public static String[] split(String string, char ch) {
            ArrayList<String> stringList = new ArrayList<String>();
            char chars[] = string.toCharArray();
            int nextStart = 0;
            for (int w = 0; w < chars.length; w++) {
                if (ch == chars[w]) {
                    stringList.add(new String(chars, nextStart, w - nextStart));
                    nextStart = w + 1;
                    if (nextStart == chars.length) { // 当最后一位是分割符的话,就再添加一个空的字符串到分割数组中去
                        stringList.add("");
                    }
                }
            }
            if (nextStart < chars.length) { // 如果最后一位不是分隔符的话,就将最后一个分割符到最后一个字符中间的左右字符串作为一个字符串添加到分割数组中去
                stringList.add(new String(chars, nextStart, chars.length - 1 - nextStart + 1));
            }
            return stringList.toArray(new String[stringList.size()]);
        }
    
        /**
         * 计算给定的字符串的长度,计算规则是:一个汉字的长度为2,一个字符的长度为1
         *
         * @param string 给定的字符串
         * @return 长度
         */
        public static int countLength(String string) {
            int length = 0;
            char[] chars = string.toCharArray();
            for (int w = 0; w < string.length(); w++) {
                char ch = chars[w];
                if (ch >= 'u0391' && ch <= 'uFFE5') {
                    length++;
                    length++;
                } else {
                    length++;
                }
            }
            return length;
        }
    
        private static char[] getChars(char[] chars, int startIndex) {
            int endIndex = startIndex + 1;
            // 如果第一个是数字
            if (Character.isDigit(chars[startIndex])) {
                // 如果下一个是数字
                while (endIndex < chars.length && Character.isDigit(chars[endIndex])) {
                    endIndex++;
                }
            }
            char[] resultChars = new char[endIndex - startIndex];
            System.arraycopy(chars, startIndex, resultChars, 0, resultChars.length);
            return resultChars;
        }
    
        /**
         * 去除字符串中所包含的空格(包括:空格(全角,半角,制表符))
         *
         * @param s
         * @return
         */
        public static String trim(String s) {
            String result = "";
            if (null != s && !"".equals(s)) {
                result = s.replaceAll(" ", "").replaceAll(" ", "").replaceAll("	", "");
            }
            return result;
        }
    
        /**
         * 去除字符串中 首尾 所包含的空格(包括:空格(全角,半角,制表符))
         *
         * @param s
         * @return
         */
        public static String trimHF(String s) {
            s = s.trim();
            while (s.startsWith(" ")) {// 这里判断是不是全角空格
                s = s.substring(1, s.length()).trim();
            }
            while (s.endsWith(" ")) {
                s = s.substring(0, s.length() - 1).trim();
            }
            while (s.startsWith("  ")) {// 这里判断是不是全角空格
                s = s.substring(1, s.length()).trim();
            }
            while (s.endsWith("    ")) {
                s = s.substring(0, s.length() - 1).trim();
            }
            return s;
        }
    
        /**
         * 去除字符串中所包含的空格(包括:空格(全角,半角)、制表符、换页符等)
         *
         * @param s
         * @return
         */
        public static String removeAllBlank(String s) {
            String result = "";
            if (null != s && !"".equals(s)) {
                result = s.replaceAll("[ *| *| *|//s*]*", "");
            }
            return result;
        }
    
        /**
         * 删除给定字符串中所有指定的字符
         *
         * @param string 源字符串
         * @param ch     要删除的字符
         * @return 删除后的字符串
         */
        public static String removeChar(String string, char ch) {
            StringBuffer sb = new StringBuffer();
            for (char cha : string.toCharArray()) {
                if (cha != ch) {
                    sb.append(cha);
                }
            }
            return sb.toString();
        }
    
        /**
         * 删除给定字符串中给定位置处的字符
         *
         * @param string 给定字符串
         * @param index  给定位置
         */
        public static String removeChar(String string, int index) {
            String result = null;
            char[] chars = string.toCharArray();
            if (index == 0) {
                result = new String(chars, 1, chars.length - 1);
            } else if (index == chars.length - 1) {
                result = new String(chars, 0, chars.length - 1);
            } else {
                result = new String(chars, 0, index) + new String(chars, index + 1, chars.length - index);
                ;
            }
            return result;
        }
    
        /**
         * 删除给定字符串中给定位置处的字符
         *
         * @param string 给定字符串
         * @param index  给定位置
         * @param ch     如果同给定位置处的字符相同,则将给定位置处的字符删除
         */
        public static String removeChar(String string, int index, char ch) {
            String result = null;
            char[] chars = string.toCharArray();
            if (chars.length > 0 && chars[index] == ch) {
                if (index == 0) {
                    result = new String(chars, 1, chars.length - 1);
                } else if (index == chars.length - 1) {
                    result = new String(chars, 0, chars.length - 1);
                } else {
                    result = new String(chars, 0, index) + new String(chars, index + 1, chars.length - index);
                    ;
                }
            } else {
                result = string;
            }
            return result;
        }
    
        /**
         * 将给定的字符串MD5加密
         *
         * @param string 给定的字符串
         * @return MD5加密后生成的字符串
         */
        public static String MD5(String string) {
            String result = null;
            try {
                char[] charArray = string.toCharArray();
                byte[] byteArray = new byte[charArray.length];
                for (int i = 0; i < charArray.length; i++) {
                    byteArray[i] = (byte) charArray[i];
                }
    
                StringBuffer hexValue = new StringBuffer();
                byte[] md5Bytes = MessageDigest.getInstance("MD5").digest(byteArray);
                for (int i = 0; i < md5Bytes.length; i++) {
                    int val = ((int) md5Bytes[i]) & 0xff;
                    if (val < 16) {
                        hexValue.append("0");
                    }
                    hexValue.append(Integer.toHexString(val));
                }
    
                result = hexValue.toString();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }
    
        /**
         * 截取一级域名
         *
         * @param urlStr 网址(路径)
         * @return
         */
        public static String cutOutUrl(String urlStr) {
            String msg = "";
            String[] str = { ".com.cn/", ".gov.cn/", ".edu.cn/", ".com.cn", ".com.", ".com/", ".cn/", ".com", ".cn", ".net",
                    ".biz", ".org", ".info", ".tv", ".mil", ".pro", ".coop" };
            try {
                if (isNotEmpty(urlStr)) {
                    for (int i = 0; i < str.length; i++) {
                        if (urlStr.indexOf(str[i]) != -1) {
                            String substring = "";
                            substring = urlStr.substring(0, urlStr.indexOf(str[i]));
                            if (substring.indexOf(".") > 1) {
                                substring = substring
                                        .substring(urlStr.substring(0, urlStr.indexOf(str[i])).lastIndexOf("."))
                                        .replace(".", "");
                            } else {
                                msg = substring;
                            }
                            msg = substring + str[i];
                            if (isNotEmpty(msg)) {
                                // 如果连接存在斜杠就去掉
                                if (msg.indexOf("/") != -1) {
                                    msg = msg.substring(0, msg.length() - 1);
                                }
                                System.out.println("cutOutUrl 截取后的结果url=======>>>> " + msg);
                                break;
                            }
                        }
    
                    }
                }
            } catch (Exception e) {
                System.out.println("cutOutUrl error" + e);
            }
            return msg;
        }
    
    /**
     * 判断是否是中日韩文字
     * @param c     要判断的字符
     * @return      true或false
     */
    private static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
            return true;
        }
        return false;
    }
    
    
    /**
     * 判断是否是数字或者是英文字母
     * @param c
     * @return
     */
    public static boolean judge(char c){
        if((c >='0' && c<='9')||(c >='a' && c<='z' ||  c >='A' && c<='Z')){
            return true;
        }
        return false;
    }
    public static boolean isMessyCode(String strName) {
        //去除字符串中的空格 制表符 换行 回车
        Pattern p = Pattern.compile("\s*|	*|
    *|
    *");
        Matcher m = p.matcher(strName);
        String after = m.replaceAll("");
        //去除字符串中的标点符号
        String temp = after.replaceAll("\p{P}", "");
        //处理之后转换成字符数组
        char[] ch = temp.trim().toCharArray();
        for (int i = 0; i < ch.length; i++) {
            char c = ch[i];
            //判断是否是数字或者英文字符
            if (!judge(c)) {
                //判断是否是中日韩文
                if (!isChinese(c)) {
                    //如果不是数字或者英文字符也不是中日韩文则表示是乱码返回true
                    return true;
                }
            }
        }
        //表示不是乱码 返回false
        return false;
    }

    }

    package com.util;
    import java.io.UnsupportedEncodingException;import java.net.URLEncoder;import java.security.MessageDigest;import java.util.ArrayList;import java.util.Locale;import java.util.regex.Matcher;import java.util.regex.Pattern;
    /** * 字符串工具类 */public class StringUtil {
    private StringUtil() {throw new AssertionError();}
    /** * <pre> * isBlank(null) = true; * isBlank("") = true; * isBlank("  ") = true; * isBlank("a") = false; * isBlank("a ") = false; * isBlank(" a") = false; * isBlank("a b") = false; * </pre> * * @param str 字符串 * @return 如果字符串为空或者长度为0,返回true,否则返回false */public static boolean isBlank(String str) {return (str == null || str.trim().length() == 0);}
    /** * <pre> * isEmpty(null) = true; * isEmpty("") = true; * isEmpty("  ") = false; * </pre> * * @param c 字符序列 * @return 如果字符序列为空或者长度为0,返回true,否则返回false */public static boolean isEmpty(CharSequence c) {return (c == null || c.length() == 0);}
    /** * 判断给定的字符串是否为null或者是空的 *  * @param string 给定的字符串 */public static boolean isEmpty(String string) {return string == null || "".equals(string.trim());}
    /** * 判断给定的字符串是否不为null且不为空 *  * @param string 给定的字符串 */public static boolean isNotEmpty(String string) {return !isEmpty(string);}
    /** * 判断给定的字符串数组中的所有字符串是否都为null或者是空的 *  * @param strings 给定的字符串 */public static boolean isEmpty(String... strings) {boolean result = true;for (String string : strings) {if (isNotEmpty(string)) {result = false;break;}}return result;}
    /** * 判断给定的字符串数组中是否全部都不为null且不为空 * * @param strings 给定的字符串数组 * @return 是否全部都不为null且不为空 */public static boolean isNotEmpty(String... strings) {boolean result = true;for (String string : strings) {if (isEmpty(string)) {result = false;break;}}return result;}
    /** * 是否全是数字 */public static boolean isAllDigital(char[] chars) {boolean result = true;for (int w = 0; w < chars.length; w++) {if (!Character.isDigit(chars[w])) {result = false;break;}}return result;}
    /** * 判断浮点数整数 int 类型 * * @param str * @return */public static boolean isInteger(String str) {if (null == str || "".equals(str)) {return false;}Pattern pattern = Pattern.compile("^[-\+]?[\d]*$");return pattern.matcher(str).matches();}
    /** * 判断浮点数(double和float) */public static boolean isDouble(String str) {if (null == str || "".equals(str)) {return false;}Pattern pattern = Pattern.compile("^[-\+]?[.\d]*$");return pattern.matcher(str).matches();}
    /** * 获取字符序列的长度 *  * <pre> * length(null) = 0; * length("") = 0; * length("abc") = 3; * </pre> * * @param c 字符序列 * @return 如果字符序列为空,返回0,否则返回字符序列的长度 */public static int length(CharSequence c) {return c == null ? 0 : c.length();}
    /** * null Object to empty string 空对象转化成空字符串 *  * <pre> * nullStrToEmpty(null) = ""; * nullStrToEmpty("") = ""; * nullStrToEmpty("aa") = "aa"; * </pre> * * @param object 对象 * @return String */public static String nullStrToEmpty(Object object) {return object == null ? "" : (object instanceof String ? (String) object : object.toString());}
    /** * 第一个字母大写 *  * @param str str * @return String */public static String capitalizeFirstLetter(String str) {if (isEmpty(str)) {return str;}char c = str.charAt(0);return (!Character.isLetter(c) || Character.isUpperCase(c)) ? str: new StringBuilder(str.length()).append(Character.toUpperCase(c)).append(str.substring(1)).toString();}
    /** * 用utf-8编码 *  * @param str 字符串 * @return 返回一个utf8的字符串 */public static String utf8Encode(String str) {if (!isEmpty(str) || str.getBytes().length != str.length()) {try {return URLEncoder.encode(str, "utf-8");} catch (UnsupportedEncodingException e) {throw new RuntimeException("UnsupportedEncodingException occurred. ", e);}}return str;}
    /** * @param href 字符串 * @return 返回一个html */public static String getHrefInnerHtml(String href) {if (isEmpty(href)) {return "";}String hrefReg = ".*<[\s]*a[\s]*.*>(.+?)<[\s]*/a[\s]*>.*";Pattern hrefPattern = Pattern.compile(hrefReg, Pattern.CASE_INSENSITIVE);Matcher hrefMatcher = hrefPattern.matcher(href);if (hrefMatcher.matches()) {return hrefMatcher.group(1);}return href;}
    /** * @param source 字符串 * @return 返回htmL到字符串 */public static String htmlEscapeCharsToString(String source) {return StringUtil.isEmpty(source) ? source: source.replaceAll("<", "<").replaceAll(">", ">").replaceAll("&", "&");//                .replaceAll(""", """); }
    /** * @param s 字符串 * @return String */public static String fullWidthToHalfWidth(String s) {if (isEmpty(s)) {return s;}char[] source = s.toCharArray();for (int i = 0; i < source.length; i++) {if (source[i] == 12288) {source[i] = ' ';// } else if (source[i] == 12290) {// source[i] = '.';} else if (source[i] >= 65281 && source[i] <= 65374) {source[i] = (char) (source[i] - 65248);} else {source[i] = source[i];}}return new String(source);}
    /** * @param s 字符串 * @return 返回的数值 */public static String halfWidthToFullWidth(String s) {
    if (isEmpty(s)) {return s;}
    char[] source = s.toCharArray();for (int i = 0; i < source.length; i++) {if (source[i] == ' ') {source[i] = (char) 12288;// } else if (source[i] == '.') {// source[i] = (char)12290;} else if (source[i] >= 33 && source[i] <= 126) {source[i] = (char) (source[i] + 65248);} else {source[i] = source[i];}}return new String(source);}
    /** * 如果字符串是null或者空就返回"" */public static String filterEmpty(String string) {return StringUtil.isNotEmpty(string) ? string : "";}
    /** * 对给定的字符串进行空白过滤 * * @param string 给定的字符串 * @return 如果给定的字符串是一个空白字符串,那么返回null;否则返回本身。 */public static String filterBlank(String string) {if ("".equals(string)) {return null;} else {return string;}}
    /** * 将给定字符串的首字母转为小写 * * @param str 给定字符串 * @return 新的字符串 */public static String firstLetterToLowerCase(String str) {return toLowerCase(str, 0, 1);}
    /** * 将给定字符串的首字母转为大写 * * @param str 给定字符串 * @return 新的字符串 */public static String firstLetterToUpperCase(String str) {return toUpperCase(str, 0, 1);}
    /** * 判断给定的字符串是否以一个特定的字符串开头,忽略大小写 * * @param sourceString 给定的字符串 * @param newString    一个特定的字符串 */public static boolean startsWithIgnoreCase(String sourceString, String newString) {int newLength = newString.length();int sourceLength = sourceString.length();if (newLength == sourceLength) {return newString.equalsIgnoreCase(sourceString);} else if (newLength < sourceLength) {char[] newChars = new char[newLength];sourceString.getChars(0, newLength, newChars, 0);return newString.equalsIgnoreCase(String.valueOf(newChars));} else {return false;}}
    /** * 判断给定的字符串是否以一个特定的字符串结尾,忽略大小写 * * @param sourceString 给定的字符串 * @param newString    一个特定的字符串 */public static boolean endsWithIgnoreCase(String sourceString, String newString) {int newLength = newString.length();int sourceLength = sourceString.length();if (newLength == sourceLength) {return newString.equalsIgnoreCase(sourceString);} else if (newLength < sourceLength) {char[] newChars = new char[newLength];sourceString.getChars(sourceLength - newLength, sourceLength, newChars, 0);return newString.equalsIgnoreCase(String.valueOf(newChars));} else {return false;}}
    /** * 检查字符串长度,如果字符串的长度超过maxLength,就截取前maxLength个字符串并在末尾拼上appendString */public static String checkLength(String string, int maxLength, String appendString) {if (string.length() > maxLength) {string = string.substring(0, maxLength);if (appendString != null) {string += appendString;}}return string;}
    /** * 检查字符串长度,如果字符串的长度超过maxLength,就截取前maxLength个字符串并在末尾拼上… */public static String checkLength(String string, int maxLength) {return checkLength(string, maxLength, "…");}
    /** * 判断输入的字符串是否全部为大写 * * @param str * @return */public static boolean checkAllUpperCase(String str) {for (int i = 0; i < str.length(); i++) {char c = str.charAt(i);if (c >= 97 && c <= 122) {return false;}}return true;}
    /** * 将给定字符串中给定的区域的字符转换成小写 * * @param str        给定字符串中 * @param beginIndex 开始索引(包括) * @param endIndex   结束索引(不包括) * @return 新的字符串 */public static String toLowerCase(String str, int beginIndex, int endIndex) {return str.replaceFirst(str.substring(beginIndex, endIndex),str.substring(beginIndex, endIndex).toLowerCase(Locale.getDefault()));}
    /** * 将给定字符串中给定的区域的字符转换成大写 * * @param str        给定字符串中 * @param beginIndex 开始索引(包括) * @param endIndex   结束索引(不包括) * @return 新的字符串 */public static String toUpperCase(String str, int beginIndex, int endIndex) {return str.replaceFirst(str.substring(beginIndex, endIndex),str.substring(beginIndex, endIndex).toUpperCase(Locale.getDefault()));}
    /** * 在给定的字符串中,用新的字符替换所有旧的字符 *  * @param string  给定的字符串 * @param oldchar 旧的字符 * @param newchar 新的字符 * @return 替换后的字符串 */public static String replace(String string, char oldchar, char newchar) {char chars[] = string.toCharArray();for (int w = 0; w < chars.length; w++) {if (chars[w] == oldchar) {chars[w] = newchar;break;}}return new String(chars);}
    /** * 替换路径中的空格为%20 * * @param str * @return */public static String replaceSpace(StringBuffer str) {for (int k = 0; k < str.length(); k++) {char index = str.charAt(k);if (index == ' ') {str.replace(k, k + 1, "%20");}}
    return str.toString();}
    /** * @param str 资源 * @return 特殊字符串切换 */public static String replaceBlanktihuan(String str) {
    String dest = "";if (str != null) {Pattern p = Pattern.compile("\s*| | | ");Matcher m = p.matcher(str);dest = m.replaceAll("");}return dest;}
    /** * 如果字符串包含%和_则需要转义\%和\_--Mysql查询包含百分号的时候用 * * @param s * @return */public static String replaceMySQLKeyWords(String s) {String result = "";if (null != s && !"".equals(s)) {result = s.replaceAll("%", "\\%").replaceAll("_", "\\_");}return result;}
    /** * 把给定的字符串用给定的字符分割 *  * @param string 给定的字符串 * @param ch     给定的字符 * @return 分割后的字符串数组 */public static String[] split(String string, char ch) {ArrayList<String> stringList = new ArrayList<String>();char chars[] = string.toCharArray();int nextStart = 0;for (int w = 0; w < chars.length; w++) {if (ch == chars[w]) {stringList.add(new String(chars, nextStart, w - nextStart));nextStart = w + 1;if (nextStart == chars.length) { // 当最后一位是分割符的话,就再添加一个空的字符串到分割数组中去stringList.add("");}}}if (nextStart < chars.length) { // 如果最后一位不是分隔符的话,就将最后一个分割符到最后一个字符中间的左右字符串作为一个字符串添加到分割数组中去stringList.add(new String(chars, nextStart, chars.length - 1 - nextStart + 1));}return stringList.toArray(new String[stringList.size()]);}
    /** * 计算给定的字符串的长度,计算规则是:一个汉字的长度为2,一个字符的长度为1 * * @param string 给定的字符串 * @return 长度 */public static int countLength(String string) {int length = 0;char[] chars = string.toCharArray();for (int w = 0; w < string.length(); w++) {char ch = chars[w];if (ch >= 'u0391' && ch <= 'uFFE5') {length++;length++;} else {length++;}}return length;}
    private static char[] getChars(char[] chars, int startIndex) {int endIndex = startIndex + 1;// 如果第一个是数字if (Character.isDigit(chars[startIndex])) {// 如果下一个是数字while (endIndex < chars.length && Character.isDigit(chars[endIndex])) {endIndex++;}}char[] resultChars = new char[endIndex - startIndex];System.arraycopy(chars, startIndex, resultChars, 0, resultChars.length);return resultChars;}
    /** * 去除字符串中所包含的空格(包括:空格(全角,半角,制表符)) * * @param s * @return */public static String trim(String s) {String result = "";if (null != s && !"".equals(s)) {result = s.replaceAll(" ", "").replaceAll(" ", "").replaceAll(" ", "");}return result;}
    /** * 去除字符串中 首尾 所包含的空格(包括:空格(全角,半角,制表符)) * * @param s * @return */public static String trimHF(String s) {s = s.trim();while (s.startsWith(" ")) {// 这里判断是不是全角空格s = s.substring(1, s.length()).trim();}while (s.endsWith(" ")) {s = s.substring(0, s.length() - 1).trim();}while (s.startsWith("  ")) {// 这里判断是不是全角空格s = s.substring(1, s.length()).trim();}while (s.endsWith("    ")) {s = s.substring(0, s.length() - 1).trim();}return s;}
    /** * 去除字符串中所包含的空格(包括:空格(全角,半角)、制表符、换页符等) * * @param s * @return */public static String removeAllBlank(String s) {String result = "";if (null != s && !"".equals(s)) {result = s.replaceAll("[ *| *| *|//s*]*", "");}return result;}
    /** * 删除给定字符串中所有指定的字符 * * @param string 源字符串 * @param ch     要删除的字符 * @return 删除后的字符串 */public static String removeChar(String string, char ch) {StringBuffer sb = new StringBuffer();for (char cha : string.toCharArray()) {if (cha != ch) {sb.append(cha);}}return sb.toString();}
    /** * 删除给定字符串中给定位置处的字符 * * @param string 给定字符串 * @param index  给定位置 */public static String removeChar(String string, int index) {String result = null;char[] chars = string.toCharArray();if (index == 0) {result = new String(chars, 1, chars.length - 1);} else if (index == chars.length - 1) {result = new String(chars, 0, chars.length - 1);} else {result = new String(chars, 0, index) + new String(chars, index + 1, chars.length - index);;}return result;}
    /** * 删除给定字符串中给定位置处的字符 * * @param string 给定字符串 * @param index  给定位置 * @param ch     如果同给定位置处的字符相同,则将给定位置处的字符删除 */public static String removeChar(String string, int index, char ch) {String result = null;char[] chars = string.toCharArray();if (chars.length > 0 && chars[index] == ch) {if (index == 0) {result = new String(chars, 1, chars.length - 1);} else if (index == chars.length - 1) {result = new String(chars, 0, chars.length - 1);} else {result = new String(chars, 0, index) + new String(chars, index + 1, chars.length - index);;}} else {result = string;}return result;}
    /** * 将给定的字符串MD5加密 * * @param string 给定的字符串 * @return MD5加密后生成的字符串 */public static String MD5(String string) {String result = null;try {char[] charArray = string.toCharArray();byte[] byteArray = new byte[charArray.length];for (int i = 0; i < charArray.length; i++) {byteArray[i] = (byte) charArray[i];}
    StringBuffer hexValue = new StringBuffer();byte[] md5Bytes = MessageDigest.getInstance("MD5").digest(byteArray);for (int i = 0; i < md5Bytes.length; i++) {int val = ((int) md5Bytes[i]) & 0xff;if (val < 16) {hexValue.append("0");}hexValue.append(Integer.toHexString(val));}
    result = hexValue.toString();} catch (Exception e) {e.printStackTrace();}return result;}
    /** * 截取一级域名 * * @param urlStr 网址(路径) * @return */public static String cutOutUrl(String urlStr) {String msg = "";String[] str = { ".com.cn/", ".gov.cn/", ".edu.cn/", ".com.cn", ".com.", ".com/", ".cn/", ".com", ".cn", ".net",".biz", ".org", ".info", ".tv", ".mil", ".pro", ".coop" };try {if (isNotEmpty(urlStr)) {for (int i = 0; i < str.length; i++) {if (urlStr.indexOf(str[i]) != -1) {String substring = "";substring = urlStr.substring(0, urlStr.indexOf(str[i]));if (substring.indexOf(".") > 1) {substring = substring.substring(urlStr.substring(0, urlStr.indexOf(str[i])).lastIndexOf(".")).replace(".", "");} else {msg = substring;}msg = substring + str[i];if (isNotEmpty(msg)) {// 如果连接存在斜杠就去掉if (msg.indexOf("/") != -1) {msg = msg.substring(0, msg.length() - 1);}System.out.println("cutOutUrl 截取后的结果url=======>>>> " + msg);break;}}
    }}} catch (Exception e) {System.out.println("cutOutUrl error" + e);}return msg;}
    }

  • 相关阅读:
    IIS 安装 pydio
    Windows环境配置Apache+Mysql+PHP
    Azure 云平台用 SQOOP 将 SQL server 2012 数据表导入 HIVE / HBASE
    PHP 启动 cURL模块以及启动失败的解决方案
    NodeJS 各websocket框架性能分析
    使用AndroidStudio编译NDK的方法及错误解决方案
    Ubuntu 系统下 mongodb 安装和配置
    Ubuntu安装nodeJS
    node.js应用Redis数据库
    Android平台相机接口的应用
  • 原文地址:https://www.cnblogs.com/adao21/p/12725972.html
Copyright © 2020-2023  润新知