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

org.kaizen4j.common.util.TextUtils Maven / Gradle / Ivy

package org.kaizen4j.common.util;

import com.google.common.collect.Lists;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.kaizen4j.common.encrypt.GZIP;

import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import static org.kaizen4j.common.base.Symbols.EMPTY;

/**
 * 字符串工具类
 *
 * @author John
 */
public final class TextUtils {

    private static final Pattern JSONP_PATTERN = Pattern.compile("\\{[\\s\\S]*(?=\\))",
                    Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);

    private static final Pattern INTEGER_PATTERN = Pattern.compile("^-?\\d+$");

    private static final Pattern FLOAT_PATTERN = Pattern.compile("^(-?\\d+)(\\.\\d+)?$");

    private static final Pattern FRACTION_PATTERN = Pattern.compile("^[1-9]{1}\\d*/[1-9]{1}\\d*$");

    private static final String EMPTY_REGEX = "\\s*[\r|\n]\\s*";

    /**
     * 根据给定的正则表达式匹配字符串一次
     *
     * @param input 待匹配的字符串
     * @param regex 正则表达式
     * @param flags 正则表达式的flags
     * @param groupCount 指定捕获组
     * @return 匹配的子字符串
     */
    public static Optional matchString(String input, String regex, int flags, int groupCount) {
        Matcher matcher = Pattern.compile(regex, flags).matcher(input);
        while (matcher.find()) {
            if (groupCount <= matcher.groupCount()) {
                return Optional.ofNullable(matcher.group(groupCount));
            }
        }
        return Optional.empty();
    }

    public static Optional matchString(String input, Pattern pattern, int groupCount) {
        Matcher matcher = pattern.matcher(input);
        while (matcher.find()) {
            if (groupCount <= matcher.groupCount()) {
                return Optional.ofNullable(matcher.group(groupCount));
            }
        }
        return Optional.empty();
    }

    /**
     * 根据给定的正则表达式匹配字符串多次
     *
     * @param input 待匹配的字符串
     * @param regex 正则表达式
     * @param flags 正则表达式的flags
     * @param groupCount 指定捕获组
     * @return 匹配的子字符串List
     */
    public static List matchStrings(String input, String regex, int flags, int groupCount) {
        List matches = Lists.newArrayList();
        Matcher matcher = Pattern.compile(regex, flags).matcher(input);

        while (matcher.find()) {
            if (groupCount <= matcher.groupCount()) {
                matches.add(matcher.group(groupCount));
            }
        }
        return matches;
    }

    public static List matchStrings(String input, Pattern pattern, int groupCount) {
        List matches = Lists.newArrayList();
        Matcher matcher = pattern.matcher(input);

        while (matcher.find()) {
            if (groupCount <= matcher.groupCount()) {
                matches.add(matcher.group(groupCount));
            }
        }
        return matches;
    }

    /**
     * 根据给定的正则表达式匹配字符串多次,不区分大小写
     *
     * @param input 待匹配的字符串
     * @param regex 正则表达式
     * @return 匹配的子字符串 List
     */
    public static List matchStrings(String input, String regex) {
        return matchStrings(input, regex, Pattern.CASE_INSENSITIVE, 0);
    }

    /**
     * 根据给定的正则表达式匹配字符串一次,不区分大小写
     *
     * @param input 待匹配的字符串
     * @param regex 正则表达式
     * @return 匹配的子字符串
     */
    public static Optional matchString(String input, String regex) {
        return matchString(input, regex, Pattern.CASE_INSENSITIVE, 0);
    }

    /**
     * 匹配指定的 JSONP 格式字符串中的JSON格式子字符串,不区分大小写
     *
     * @param jsonp JSONP 字符串
     * @return JSON 子字符串
     */
    public static Optional matchJsonString(String jsonp) {
        return matchString(jsonp, JSONP_PATTERN, 0);
    }

    public static boolean isInt(String input) {
        Matcher matcher = INTEGER_PATTERN.matcher(input);
        return matcher.matches();
    }

    public static boolean isFloat(String input) {
        Matcher matcher = FLOAT_PATTERN.matcher(input);
        return matcher.matches();
    }

    /**
     * 是否是分数
     *
     * @param value
     * @return boolean
     */
    public static boolean isFraction(String value) {
        return FRACTION_PATTERN.matcher(value).matches();
    }

    /**
     * 压缩字符串
     *
     * @param input 待压缩字符串
     * @return String
     */
    public static String compress(String input) {
        return Base64.encodeBase64String(GZIP.compress(input));
    }

    /**
     * 解压缩字符串
     *
     * @param compressed 已压缩字符串
     * @return String
     */
    public static String decompress(String compressed) {
        return GZIP.decompress(Base64.decodeBase64(compressed));
    }

    /**
     * 连接多个字符串
     *
     * @param strings 字符串参数列表
     * @return 连接后的字符串
     */
    public static String concat(String... strings) {
        StringBuilder content = new StringBuilder();
        Stream.of(strings).forEach(str -> content.append(str));
        return content.toString();
    }

    /**
     * 使字符串在一行显示
     *
     * @param string 待处理字符串
     * @return 在一行显示的字符串
     */
    public static String toInline(String string) {
        if (StringUtils.isEmpty(string)) {
            return string;
        }
        return string.replaceAll(EMPTY_REGEX, EMPTY);
    }

    public static String replaceEmptyString(String string) {
        if (StringUtils.isEmpty(string)) {
            return string;
        }
        return string.replaceAll("\\s+", EMPTY);
    }

    /**
     * 生成字符串的哈希码
     *
     * @param strings
     * @return 哈希码
     */
    public static int hashCode(String... strings) {
        return new HashCodeBuilder(11, 31).append(strings).toHashCode();
    }

    /**
     * 判断字符是否是中日韩字符
     * 
     * @param c 字符
     * @return 是返回 true,否则返回 false
     */
    public static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        return 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;
    }

    /**
     * 判断字符串是否乱码
     * 
     * @param strName
     * @return 乱码返回 true,否则返回 false
     */
    public static boolean isMessyCode(String strName) {
        Pattern p = Pattern.compile("\\s*|\t*|\r*|\n*");
        Matcher m = p.matcher(strName);

        String after = m.replaceAll("");
        String temp = after.replaceAll("\\p{P}", "");

        char[] ch = temp.trim().toCharArray();
        float chLength = ch.length;
        float count = 0;

        for (int i = 0; i < ch.length; i++) {
            char c = ch[i];
            if (!Character.isLetterOrDigit(c)) {

                if (!isChinese(c)) {
                    count = count + 1;
                }
            }
        }

        float result = count / chLength;
        return result > 0.4;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy