org.javaweb.utils.StringUtils.java Source code

Java tutorial

Introduction

Here is the source code for org.javaweb.utils.StringUtils.java

Source

/*
 * Copyright yz 2016-01-14  Email:admin@javaweb.org.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.javaweb.utils;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils extends org.apache.commons.lang.StringUtils {

    /**
     * ?,?
     *
     * @param obj
     * @return
     */
    public static boolean isNotEmpty(Object obj) {
        if (obj == null) {
            return false;
        }

        return !"".equals(String.valueOf(obj).trim());
    }

    /**
     * ??
     *
     * @param str
     * @return
     */
    public static boolean isNum(String str) {
        return isNotEmpty(str) && str.trim().replaceAll("[0-9]+", "").length() == 0;
    }

    /**
     * ?
     *
     * @param array
     * @param separator
     * @return
     */
    public static String join(Object[] array, String separator) {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                sb.append(separator);
            }
            if (array[i] != null) {
                sb.append(array[i]);
            }
        }

        return sb.toString();
    }

    /**
     * List?
     *
     * @param ls
     * @param separator
     * @return
     */
    public static String join(List<?> ls, String separator) {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < ls.size(); i++) {
            if (i > 0) {
                sb.append(separator);
            }

            if (ls.get(i) != null) {
                sb.append(ls.get(i));
            }
        }

        return sb.toString();
    }

    /**
     * ??
     *
     * @param array
     * @param objectToFind
     * @return
     */
    public static boolean arrayContains(Object[] array, Object objectToFind) {
        for (Object array1 : array) {
            if (array1 != null && ("" + array1).contains("" + objectToFind)) {
                return true;
            }
        }

        return false;
    }

    /**
     * ??
     *
     * @return
     */
    public static String getSeparator() {
        if ("\r".equals(System.getProperty("line.separator"))) {
            return "\\r";//linux
        } else if ("\r\n".equals(System.getProperty("line.separator"))) {
            return "\\r\\n";//windows
        } else {
            return "\\n";//mac
        }
    }

    /**
     * ?uuid
     *
     * @return
     */
    public static synchronized String getUUID() {
        return UUID.randomUUID().toString().toLowerCase().replaceAll("-", "");
    }

    /**
     * unicode ?? utf-8
     *
     * @param str
     * @return
     */
    public static String unicodeToUtf8(String str) {
        if (str == null || str.length() < 1 || !Pattern.compile("\\\\u").matcher(str).find()) {
            return str;
        }

        char aChar;
        int len = str.length();
        StringBuilder outBuffer = new StringBuilder(len);

        for (int x = 0; x < len;) {
            aChar = str.charAt(x++);
            if (aChar == '\\') {
                aChar = str.charAt(x++);
                if (aChar == 'u') {
                    int value = 0;
                    for (int i = 0; i < 4; i++) {
                        aChar = str.charAt(x++);
                        switch (aChar) {
                        case '0':
                        case '1':
                        case '2':
                        case '3':
                        case '4':
                        case '5':
                        case '6':
                        case '7':
                        case '8':
                        case '9':
                            value = (value << 4) + aChar - '0';
                            break;
                        case 'a':
                        case 'b':
                        case 'c':
                        case 'd':
                        case 'e':
                        case 'f':
                            value = (value << 4) + 10 + aChar - 'a';
                            break;
                        case 'A':
                        case 'B':
                        case 'C':
                        case 'D':
                        case 'E':
                        case 'F':
                            value = (value << 4) + 10 + aChar - 'A';
                            break;
                        default:
                            throw new IllegalArgumentException("Malformed   \\uxxxx   encoding.");
                        }
                    }

                    outBuffer.append((char) value);
                } else {
                    switch (aChar) {
                    case 't':
                        aChar = '\t';
                        break;
                    case 'r':
                        aChar = '\r';
                        break;
                    case 'n':
                        aChar = '\n';
                        break;
                    case 'f':
                        aChar = '\f';
                        break;
                    default:
                        break;
                    }
                    outBuffer.append(aChar);
                }
            } else {
                outBuffer.append(aChar);
            }
        }

        return outBuffer.toString();
    }

    /**
     * ??
     *
     * @return
     */
    public static String getCurrentTime() {
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    }

    /**
     * ???
     *
     * @param account
     * @return
     */
    public static boolean accountValidate(String account) {
        return Pattern.compile("^[a-zA-Z0-9|-|_]{2,20}$").matcher(account).find();
    }

    /**
     * ?
     *
     * @param mail
     * @return
     */
    public static boolean mailValidate(String mail) {
        return Pattern.compile("^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$").matcher(mail)
                .find();
    }

    /**
     * ?
     *
     * @param cellPhone
     * @return
     */
    public static boolean cellPhoneValidate(String cellPhone) {
        return Pattern.compile("^1[3|4|5|7|8][0-9]\\d{8}$").matcher(cellPhone).find();
    }

    /**
     * ?
     *
     * @param str
     * @return
     */
    public static String toUpperCaseFirstOne(String str) {
        if (isNotEmpty(str)) {
            char f = str.charAt(0);

            if (Character.isUpperCase(f)) {
                return str;
            } else {
                return str.replaceFirst(String.valueOf(f), String.valueOf(f).toUpperCase());
            }
        }

        return str;
    }

    /**
     * Exception ??
     *
     * @param e
     * @return
     */
    public static String exceptionToString(Exception e) {
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw, true));

        return sw.toString();
    }

    public static String getSpecialRandomString(int length) {
        String[] words = new String[] { "1", "I", "i", "l", "L", "0", "O", "o" };
        return randomString(length, words);
    }

    public static String getRandomString(int length) {
        return randomString(length);
    }

    /**
     * ?
     *
     * @param length ?
     * @param words  ???
     * @return
     */
    public static String randomString(int length, String... words) {
        if (length < 1) {
            return null;
        }

        String str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

        if (words.length > 0) {
            for (String word : words) {
                str = str.replace(word, "");
            }
        }

        Random random = new Random();
        char[] strs = str.toCharArray();
        char[] chars = new char[length];

        for (int i = 0; i < chars.length; i++) {
            chars[i] = strs[random.nextInt(strs.length)];
        }

        return new String(chars);
    }

    /**
     * ?
     *
     * @param content 
     * @param sub     ??
     * @param regex   ???
     * @return
     */
    public static int textIndexOfCount(String content, String sub, boolean regex) {
        Matcher matcher = Pattern.compile(regex ? sub : Pattern.quote(sub)).matcher(content);
        int i = 0;

        while (matcher.find()) {
            i++;
        }

        return i;
    }

}