All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.rt.core.util.StringUtil Maven / Gradle / Ivy

There is a newer version: 1.1.17
Show newest version
package com.rt.core.util;

import com.json.JSONArray;
import com.rt.core.constant.RTConst;
import com.rt.core.log.Log;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.CharUtils;
import org.apache.commons.lang3.math.NumberUtils;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串工具类
 * 扩展org.apache.commons.lang.StringUtils
 */
class StringUtil extends org.apache.commons.lang3.StringUtils {

    private static Log log = Log.getLog(StringUtil.class);

    /**
     * 删除所有空白字符
     *
     * @param value value
     * @return String
     */
    public static String trimSpace(Object value) {
        return val(value).replaceAll("\\s*", EMPTY);
    }

    /**
     * String - InputStream
     *
     * @param value (default encoding UTF-8)
     * @return InputStream
     */
    public static InputStream toInputStream(String value) {
        try {
            return new ByteArrayInputStream(value.getBytes(RTConst.UTF_8));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * String - InputStream
     *
     * @param value value
     * @param encode encode
     * @return InputStream
     * @throws Exception Exception
     */
    public static InputStream toInputStream(String value, String encode)
            throws Exception {
        return new ByteArrayInputStream(value.getBytes(encode));
    }

    /**
     * inputStream - String
     *
     * @param is is
     * @return String
     * @throws IOException IOException
     */
    public static String inputStreamToString(InputStream is) throws IOException {
        return IOUtils.toString(is, RTConst.UTF_8);
    }

    /**
     * inputStream - String
     *
     * @param is is
     * @param encoding encoding
     * @return String
     * @throws IOException IOException
     */
    public static String inputStreamToString(InputStream is, String encoding)
            throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(is,
                encoding));
        StringBuffer buffer = new StringBuffer();
        String line = RTConst.EMPTY;
        while ((line = in.readLine()) != null) {
            buffer.append(line);
        }
        return buffer.toString();
    }

    /**
     * 比较long值是否相等
     *
     * @param value1 value1
     * @param value2 value2
     * @return boolean
     */
    public static boolean equals(Long value1, Long value2) {
        return value1 != null && value2 != null
                && value1.longValue() == value2.longValue();
    }

    /**
     * 默认使用 ',' 切割
     *
     * @param str str
     * @return String[]
     */
    public static String[] split(String str) {
        if (str == null) {
            return null;
        }
        return str.split(RTConst.COMMA);
    }

    /**
     * 是否超出ascii码范围
     *
     * @param value value
     * @return hasAscii
     */
    public static boolean isAscii(String value) {
        char[] csrc = value.toCharArray();
        for (char aCsrc : csrc) {
            int iCode = (int) aCsrc;
            if (iCode > 127) {
                return true;
            }
        }
        return false;
    }

    /**
     * NumberUtils.isNumber("12.3") = true
     * NumberUtils.isNumber("1,112.3") = false
     *
     * @param value value
     * @return boolean
     */
    public static boolean isNumber(String value) {
        return NumberUtils.isNumber(value);
    }

    /**
     * Checks if the String contains only unicode digits. A decimal point is not
     * a unicode digit and returns false.
     * null will return false. An empty String ("")
     * will return false.
     * StringUtils.isNumeric(null)   = false
     * StringUtils.isNumeric("")     = false
     * StringUtils.isNumeric("  ")   = false
     * StringUtils.isNumeric("123")  = true
     * StringUtils.isNumeric("12 3") = false
     * StringUtils.isNumeric("ab2c") = false
     * StringUtils.isNumeric("12-3") = false
     * StringUtils.isNumeric("12.3") = false
     *
     * @param str the String to check, may be null
     * @return true if only contains digits, and is non-null
     */
    public static boolean isNumeric(String str) {
        if (isEmpty(str)) {
            return false;
        }
        int sz = str.length();
        for (int i = 0; i < sz; i++) {
            if (Character.isDigit(str.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }

    /**
     * null转为String值为""
     *
     * @param obj obj
     * @return String
     */
    public static String val(Object obj) {
        if (obj == null || EMPTY.equals(obj)) {
            return EMPTY;
        }
        if (obj instanceof String) {
            return (String) obj;
        }
        return obj.toString();
    }

    /**
     * null转为String值为""
     *
     * @param obj obj
     * @param defaultValue defaultValue
     * @return String
     */
    public static String val(Object obj, Object defaultValue) {
        if (obj == null || EMPTY.equals(obj)) {
            if (defaultValue == null) {
                return EMPTY;
            }
            if (defaultValue instanceof String) {
                return (String) defaultValue;
            }
            return defaultValue.toString();
        } else {
            return obj.toString();
        }
    }

    /**
     * null转为String值为""
     *
     * @param obj obj
     * @return String
     */
    private static String nullToNull(Object obj) {
        if (obj == null || EMPTY.equals(obj)) {
            return null;
        }
        if (obj instanceof String) {
            return (String) obj;
        }
        return obj.toString();
    }

    /**
     * 简单字符串转换.
     *
     * @param str str
     * @param charset charset
     * @param tochar tochar
     * @return String
     */
    public static String charSet(String str, String charset, String tochar) {
        String s = null;
        try {
            s = new String(str.getBytes(charset), tochar);
        } catch (UnsupportedEncodingException e) {
            return s;
        }
        return s;
    }

    /**
     * 简单字符串转换.
     *
     * @param str str
     * @param charset charset
     * @return String
     */
    public static String charSet(String str, String charset) {
        String s = null;
        try {
            s = new String(str.getBytes(charset));
        } catch (UnsupportedEncodingException e) {
            return s;
        }
        return s;
    }

    /**
     * URL
     *
     * @param str str
     * @return String
     */
    public static String urlCharSet(String str) {
        return charSet(str, RTConst.ISO_8859_1, RTConst.UTF_8);
    }

    /**
     * 对象是否为 null 或 空串
     *
     * @param obj obj
     * @return boolean
     */
    public static boolean isEmpty(Object obj) {
        if (obj == null) {
            return true;
        }
        if (obj instanceof Map) {
            return ((Map) obj).isEmpty();
        } else if (obj instanceof List) {
            return ((Collection) obj).isEmpty();
        } else if (obj.getClass().isArray()) {
            return ((Object[]) obj).length == 0;
        } else {
            return RTConst.EMPTY.equals(obj.toString());
        }
    }

    /**
     * 对象是否不为空
     *
     * @param obj obj
     * @return boolean
     */
    public static boolean isNotEmpty(Object obj) {
        return !(isEmpty(obj));
    }

    /**
     * 对象是否为 null 或 空串
     *
     * @param obj obj
     * @return boolean
     */
    public static boolean isTrimEmpty(Object obj) {
        return null == obj || RTConst.EMPTY.equals(obj.toString().trim());
    }

    /**
     * 对象是否不为空
     *
     * @param obj obj
     * @return boolean
     */
    public static boolean isTrimNotEmpty(Object obj) {
        return !(isTrimEmpty(obj));
    }

    /**
     * 换为BASE64编码字符串
     *
     * @param bytes bytes
     * @return String
     */
    public static String toBase64(byte[] bytes) {
        return RTBase64.encode(bytes);
    }

    /**
     * 字符串转换为BASE64编码字符串
     *
     * @param str str
     * @return String
     */
    public static String toBase64(String str) {
        return toBase64(str, RTConst.UTF_8);
    }

    /**
     * 字符串转换为BASE64编码字符串
     *
     * @param str str
     * @param charsetName charsetName
     * @return String
     */
    public static String toBase64(String str, String charsetName) {
        try {
            return RTBase64.encode(str.getBytes(charsetName));
        } catch (Exception e) {
            log.error(e);
            return null;
        }
    }

    /**
     * 字符串转换为BASE64编码的URL字符串
     *
     * @param  str str
     * @return String
     */
    public static String toBase64URL(String str) {
        return toURLEncoder(toBase64(str));
    }

    /**
     * 判断对象是否为true
     *
     * @param obj obj
     * @return boolean
     */
    public static boolean isTrue(Object obj) {
        return toboolean(obj) == true ? true : false;
    }

    /**
     * 判断对象是否为false
     *
     * @param obj obj
     * @return boolean
     */
    public static boolean isFalse(Object obj) {
        return toboolean(obj) == false ? true : false;
    }

    /**
     * String转换为boolean,代默认值.
     *
     * @param obj obj
     * @return boolean
     */
    public static boolean toboolean(Object obj) {
        return toBoolean(obj).booleanValue();
    }

    /**
     * String转换为boolean,代默认值.
     *
     * @param obj obj
     * @return Boolean
     */
    public static Boolean toBoolean(Object obj) {
        if (obj instanceof Boolean) {
            return (Boolean) obj;
        }
        return Boolean.valueOf(nullToNull(obj));
    }

    /**
     * String到double
     *
     * @param value value
     * @return double默认值为0
     */
    public static double todouble(Object value) {
        return todouble(value, 0);
    }

    /**
     * String到double
     *
     * @param value value
     * @param defaultValue defaultValue
     * @return double
     */
    public static double todouble(Object value, double defaultValue) {
        if (value instanceof Double) {
            return (Double) value;
        }
        if (isEmpty(value)) {
            return defaultValue;
        }
        String v = nullToNull(value);
        if (isNumber(v)) {
            return NumberUtil.toDouble(v, defaultValue);
        }
        BigDecimal bd = toBigDecimal(value);
        if (bd == null) {
            return defaultValue;
        }
        return bd.doubleValue();
    }

    /**
     * String到double
     *
     * @param value value
     * @return double默认值为null
     */
    public static Double toDouble(Object value) {
        if (value instanceof Double) {
            return (Double) value;
        }
        if (isEmpty(value)) {
            return null;
        }
        String v = nullToNull(value);
        if (isNumber(v)) {
            return NumberUtil.toDouble(v);
        }
        BigDecimal bd = toBigDecimal(value);
        if (bd == null) {
            return null;
        }
        return bd.doubleValue();
    }

    /**
     * String到double
     *
     * @param value value
     * @param defaultValue defaultValue
     * @return double
     */
    public static Double toDouble(Object value, double defaultValue) {
        return todouble(value, defaultValue);
    }

    /**
     * String到int
     *
     * @param value value
     * @return int默认值为0
     */
    public static int toInt(Object value) {
        return toInt(value, 0);
    }

    /**
     * String到int
     *
     * @param value value
     * @param defaultValue defaultValue
     * @return int 默认值为0
     */
    public static int toInt(Object value, int defaultValue) {
        if (value instanceof Integer) {
            return (Integer) value;
        }
        if (isEmpty(value)) {
            return defaultValue;
        }
        String v = nullToNull(value);
        if (isNumeric(v)) {
            return NumberUtil.toInt(v, defaultValue);
        }
        BigDecimal bd = toBigDecimal(value);
        if (bd == null) {
            return defaultValue;
        }
        return bd.intValue();
    }

    /**
     * string 转为Integer
     *
     * @param value value
     * @return Integer
     */
    public static Integer toInteger(Object value) {
        if (value instanceof Integer) {
            return (Integer) value;
        }
        if (isEmpty(value)) {
            return null;
        }
        String v = nullToNull(value);
        if (isNumeric(v)) {
            return NumberUtil.toInt(v);
        }
        BigDecimal bd = toBigDecimal(value);
        if (bd == null) {
            return null;
        }
        return bd.intValue();
    }

    /**
     * string 转为Integer
     *
     * @param value value
     * @param defaultValue defaultValue
     * @return Integer
     */
    public static Integer toInteger(Object value, int defaultValue) {
        return toInt(value, defaultValue);
    }

    /**
     * String转换为long
     *
     * @param value value
     * @return long
     */
    public static long tolong(Object value) {
        return tolong(value, 0);
    }

    /**
     * String转换为long
     *
     * @param value value
     * @param defaultValue defaultValue
     * @return long
     */
    public static long tolong(Object value, long defaultValue) {
        if (value instanceof Long) {
            return (Long) value;
        }
        if (isEmpty(value)) {
            return defaultValue;
        }
        String v = nullToNull(value);
        if (isNumeric(v)) {
            return NumberUtil.toLong(v);
        }
        BigDecimal bd = toBigDecimal(value);
        if (bd == null) {
            return defaultValue;
        }
        return bd.longValue();
    }

    /**
     * String转换为Long.
     *
     * @param value value
     * @return Long
     */
    public static Long toLong(Object value) {
        if (value instanceof Long) {
            return (Long) value;
        }
        if (isEmpty(value)) {
            return null;
        }
        String v = nullToNull(value);
        if (isNumeric(v)) {
            return NumberUtil.toLong(v);
        }
        BigDecimal bd = toBigDecimal(value);
        if (bd == null) {
            return null;
        }
        return bd.longValue();
    }

    /**
     * String转换为Long.
     *
     * @param value value
     * @param defaultValue defaultValue
     * @return Long
     */
    public static Long toLong(Object value, long defaultValue) {
        return tolong(value, defaultValue);
    }

    /**
     * String转换为BigDecimal
     *
     * @param value value
     * @return BigDecimal
     */
    public static BigDecimal toBigDecimal(Object value) {
        if (value instanceof BigDecimal) {
            return (BigDecimal) value;
        }
        if (RTUtil.isEmpty(value)) {
            return null;
        }
        try {
            return new BigDecimal(value.toString());
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * String转换为BigDecimal,代默认值.
     *
     * @param value value
     * @param defaultValue defaultValue
     * @return BigDecimal
     */
    public static BigDecimal toBigDecimal(Object value, long defaultValue) {
        if (value instanceof BigDecimal) {
            return (BigDecimal) value;
        }
        if (RTUtil.isEmpty(value)) {
            return BigDecimal.valueOf(defaultValue);
        }
        try {
            return new BigDecimal(value.toString());
        } catch (Exception e) {
            return BigDecimal.valueOf(defaultValue);
        }
    }

    /**
     * String转换为BigDecimal,代默认值.
     *
     * @param value value
     * @param defaultValue defaultValue
     * @return BigDecimal
     */
    public static BigDecimal toBigDecimal(Object value, double defaultValue) {
        if (value instanceof BigDecimal) {
            return (BigDecimal) value;
        }
        if (RTUtil.isEmpty(value)) {
            return BigDecimal.valueOf(defaultValue);
        }
        try {
            return new BigDecimal(value.toString());
        } catch (Exception e) {
            return BigDecimal.valueOf(defaultValue);
        }
    }

    /**
     * String转换为BigDecimal,代默认值.
     *
     * @param value value
     * @param defaultValue defaultValue
     * @return BigDecimal
     */
    public static BigDecimal toBigDecimal(Object value, BigDecimal defaultValue) {
        if (value instanceof BigDecimal) {
            return (BigDecimal) value;
        }
        if (RTUtil.isEmpty(value)) {
            return defaultValue;
        }
        try {
            return new BigDecimal(value.toString());
        } catch (Exception e) {
            return defaultValue;
        }
    }

    /**
     * String to unicode
     *
     * @param str str
     * @return unicode
     */
    public static String toUnicode(String str) {
        if (isEmpty(str)) {
            return EMPTY;
        }
        StringBuffer s = new StringBuffer();
        char[] c = str.toCharArray();
        for (char aC : c) {
            if (aC > 127) {
                s.append(CharUtils.unicodeEscaped(aC));
            } else {
                s.append(aC);
            }
        }
        return s.toString();
    }

    /**
     * 字符串转换为URL编码.
     *
     * @param code code
     * @return urlEncoder
     */
    public static String toURLEncoder(String code) {
        return toURLEncoder(code, RTConst.UTF_8);
    }

    /**
     * 字符串转换为URL编码.
     *
     * @param code code
     * @param charSet charSet
     * @return urlEncoder
     */
    public static String toURLEncoder(String code, String charSet) {
        try {
            return URLEncoder.encode(code, charSet);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 将字符串中的非标准ascii码过滤为ascii码.
     *
     * @param in in
     * @return ascii
     */
    public static String toAscii(String in) {
        char[] csrc = in.toCharArray();
        StringBuffer code = new StringBuffer();
        for (char aCsrc : csrc) {
            int iCode = (int) aCsrc;
            if (iCode > 127) {
                code.append(RTConst.ASCII_L);
                code.append(iCode);
                code.append(RTConst.ASCII_R);
            } else {
                code.append(aCsrc);
            }
        }
        return code.toString();
    }

    /**
     * ascii以外的字符全部转换成Hex
     *
     * @param in in
     * @return HexString
     */
    public static String toHex(String in) {
        char[] csrc = in.toCharArray();
        StringBuffer code = new StringBuffer();
        for (char aCsrc : csrc) {
            String hex = Integer.toHexString(aCsrc);
            if (hex.length() <= 2) {
                code.append(aCsrc);
            } else {
                code.append("\\u");
                code.append(hex);
            }
        }
        return code.toString();
    }

    /**
     * 两个String型数组相减 返回在数组1中存在而数组2中不存在的元素组成的新数组
     *
     * @param arr1 arr1
     * @param arr2 arr2
     * @return String[]
     */
    public static String[] arrayDiff(String[] arr1, String[] arr2) {
        if (arr1 == null) {
            return new String[0];
        }
        if (arr2 == null) {
            return arr1;
        }
        List list = new JSONArray();
        List list2 = Arrays.asList(arr2);
        for (String anArr1 : arr1) {
            if (!list2.contains(anArr1)) {
                list.add(anArr1);
            }
        }
        return (String[]) list.toArray(new String[list.size()]);
    }

    /**
     * 拼凑字符串,以split分割
     *
     * @param str str
     * @param split split
     * @return StringBuffer
     */
    public static StringBuffer arrayToString(String[] str, String split) {
        StringBuffer sb = new StringBuffer();
        if (str != null) {
            for (int i = 0; i < str.length; i++) {
                if (i != 0) {
                    sb.append(split);
                }
                sb.append(str[i]);
            }
        }
        return sb;
    }

    /**
     * ascii码到string.
     *
     * @param ascii str
     * @return string
     */
    public static String asciiToString(String ascii) {
        // 处理字符串
        String[] as = ascii.trim().split(RTConst.ASCII_R);
        StringBuffer str = new StringBuffer();
        for (String a : as) {
            int sp = a.indexOf(RTConst.ASCII_L);
            if (sp == -1) {
                str.append(a);
            } else {
                str.append(a.substring(0, sp));
                str.append((char) Integer.parseInt(a.substring(sp,
                        a.length()).replaceAll(RTConst.ASCII_L,
                        RTConst.EMPTY)));
            }
        }
        return str.toString();
    }

    /**
     * 是否在ascii码范围内
     *
     * @param c c
     * @return boolean
     */
    public static boolean isAscii(int c) {
        return c > 127 ? false : true;
    }

    /**
     * BASE64编码字符串转换为字符串
     *
     * @param base64 base64
     * @return String
     */
    public static byte[] base64ToByte(byte[] base64) {
        try {
            return RTBase64.decode(base64).getBytes(RTConst.UTF_8);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * BASE64编码字符串转换为字符串
     *
     * @param base64 base64
     * @return String
     */
    public static byte[] base64ToByte(String base64) {
        try {
            return RTBase64.decode(base64.getBytes(RTConst.UTF_8)).getBytes(
                    RTConst.UTF_8);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * base64 To String
     *
     * @param base64 base64
     * @return String
     */
    public static String base64ToString(String base64) {
        try {
            return RTBase64.decode(base64.getBytes(RTConst.UTF_8));
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * base64 To String
     *
     * @param base64 base64
     * @return String
     */
    public static String base64ToString(byte[] base64) {
        return RTBase64.decode(base64);
    }

    /**
     * BASE64编码的URL字符串转换为字符串
     *
     * @param base64Url base64Url
     * @param charSet charSet
     * @return byte[]
     */
    public static byte[] base64URLToByte(String base64Url, String charSet) {
        try {
            return base64ToByte(urlEncoderToString(base64Url, charSet));
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * BASE64编码的URL字符串转换为字符串
     *
     * @param base64Url base64Url
     * @param charSet charSet
     * @return String
     */
    public static String base64URLToString(String base64Url, String charSet) {
        return base64ToString(urlEncoderToString(base64Url, charSet));
    }

    /**
     * BASE64编码的URL字符串转换为字符串
     *
     * @param base64Url base64Url
     * @return String
     */
    public static String base64URLToString(String base64Url) {
        return base64ToString(urlEncoderToString(base64Url, RTConst.UTF_8));
    }

    /**
     * 将byte数组转换为表示16进制值的字符串
     *
     * @param array array
     * @return String
     */
    public static String toHex(byte[] array) {
        if (array == null) {
            return null;
        }
        int line = array.length;
        // 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
        StringBuffer sb = new StringBuffer(line * 2);
        for (byte anArray : array) {
            int tmp = anArray;
            // 把负数转换为正数
            while (tmp < 0) {
                tmp = tmp + 256;
            }
            // 小于0F的数需要在前面补0
            if (tmp < 16) {
                sb.append("0");
            }
            sb.append(Integer.toString(tmp, 16));
        }
        return sb.toString();
    }

    /**
     * 将表示16进制值的字符串转换为byte数组
     *
     * @param byteArray byteArray
     * @return byte[]
     */
    public static byte[] toByte(byte[] byteArray) {
        int line = byteArray.length;

        // 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
        byte[] rs = new byte[line / 2];
        for (int i = 0; i < line; i = i + 2) {
            String tmp = new String(byteArray, i, 2);
            rs[i / 2] = (byte) Integer.parseInt(tmp, 16);
        }
        return rs;
    }

    /**
     * 将表示16进制值的字符串转换为byte数组
     *
     * @param str str
     * @return byte[]
     */
    public static byte[] hexToByte(String str) {
        byte[] array = str.getBytes();
        int line = array.length;

        // 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
        byte[] rs = new byte[line / 2];
        for (int i = 0; i < line; i = i + 2) {
            String tmp = new String(array, i, 2);
            rs[i / 2] = (byte) Integer.parseInt(tmp, 16);
        }
        return rs;
    }

    /**
     * 16进制转为String
     *
     * @param str str
     * @return String
     */
    public static String hexToString(String str) {
        return String.valueOf(hexToByte(str));
    }

    /**
     * 比较字符串中是否包含符号以外的内容.
     *
     * @param in in
     * @return boolean
     */
    public static boolean hasChar(String in) {
        char[] csrc = in.toCharArray();
        for (char aCsrc : csrc) {
            int iCode = (int) aCsrc;
            if ((iCode >= 48 && iCode <= 57) || (iCode >= 65 && iCode <= 90)
                    || (iCode >= 97 && iCode <= 122) || (iCode > 127)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 获得最后一个切割符后的字符串
     *
     * @param  str str
     * @param  separator separator
     * @return String
     */
    public static String lastSeparator(String str, String separator) {
        int i = str.lastIndexOf(separator) + 1;
        return str.substring(i, str.length());
    }

    /**
     * 获得符合表达式的字符串内容
     *
     * @param regx regx
     * @param content content
     * @return Matcher
     */
    public static Matcher matcher(String regx, String content) {
        Pattern pattern = Pattern.compile(regx, Pattern.CASE_INSENSITIVE);
        return pattern.matcher(content);
    }

    /**
     * 获得符合表达式的字符串内容
     *
     * @param regx regx
     * @param content content
     * @param groupIndex groupIndex
     * @return String
     */
    public static String matcher(String regx, String content, int groupIndex) {
        List ls = new JSONArray();
        Pattern pattern = Pattern.compile(regx, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            ls.add(matcher.group(groupIndex));
        }
        if (ls.size() > 0) {
            return (String) ls.get(0);
        }
        return EMPTY;
    }

    /**
     * 获得符合表达式的字符串内容
     *
     * @param regx regx
     * @param content content
     * @param groupIndex groupIndex
     * @return List
     */
    public static List matcherList(String regx, String content, int groupIndex) {
        List ls = new JSONArray();
        Pattern pattern = Pattern.compile(regx, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            ls.add(matcher.group(groupIndex));
        }
        return ls;
    }

    /**
     * 获得符合表达式的字符串内容
     *
     * @param regx regx
     * @param content content
     * @param groupIndex groupIndex
     * @return List
     */
    public static List matcherList(String regx, String content, int[] groupIndex) {
        List ls = new JSONArray();
        Pattern pattern = Pattern.compile(regx, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            String[] gc = new String[groupIndex.length];
            for (int i = 0; i < groupIndex.length; i++) {
                gc[i] = matcher.group(groupIndex[i]);
            }
            ls.add(gc);
        }
        return ls;
    }

    /**
     * unicode to String
     *
     * @param str str
     * @return String
     */
    public static String unicodeToString(String str) {
        if (str == null) {
            return EMPTY;
        }
        List ml = matcherList(RTConst.UNICODE, str, 1);
        for (Object aMl : ml) {
            String u = (String) aMl;
            char c = (char) Integer.parseInt(u.substring(2), 16);
            str = str.replaceFirst(RTConst.UNICODE, String.valueOf(c));
        }
        return str;
    }

    /**
     * URL编码转换为字符串.
     *
     * @param code code
     * @return string
     */
    public static String urlEncoderToString(String code) {
        return urlEncoderToString(code, RTConst.UTF_8);
    }

    /**
     * URL编码转换为字符串.
     *
     * @param code code
     * @param charSet charSet
     * @return string
     */
    public static String urlEncoderToString(String code, String charSet) {
        try {
            return URLDecoder.decode(code, charSet);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 首字母小写
     *
     * @param str str
     * @return String
     */
    public static String toFirstLowerCase(String str) {
        if (isEmpty(str)) {
            return str;
        } else {
            return str.substring(0, 1).toLowerCase()
                    + str.substring(1, str.length());
        }
    }

    /**
     * 首字符大写
     *
     * @param str str
     * @return String
     */
    public static String toFirstUpperCase(String str) {
        if (isEmpty(str)) {
            return str;
        } else {
            return str.substring(0, 1).toUpperCase() + str.substring(1);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy