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

com.feingto.cloud.kit.StringKit Maven / Gradle / Ivy

There is a newer version: 2.5.2.RELEASE
Show newest version
package com.feingto.cloud.kit;

import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.io.LineNumberReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 字符串转换工具
 *
 * @author longfei
 */
@Slf4j
public class StringKit {
    /**
     * 逗号分割字符串
     *
     * @param str String 原始字符串
     * @return String[] 分割后的字符串数组
     */
    public static String[] split(String str) {
        return split(str, ",");
    }

    /**
     * 分割字符串
     *
     * @param str   String 原始字符串
     * @param split String 分隔符
     * @return String[] 分割后的字符串数组
     */
    public static String[] split(String str, String split) {
        int index;
        if (Objects.isNull(str) || Objects.isNull(split)) {
            return null;
        }
        ArrayList al = new ArrayList<>();
        while ((index = str.indexOf(split)) != -1) {
            al.add(str.substring(0, index));
            str = str.substring(index + split.length());
        }
        al.add(str);
        return al.toArray(new String[0]);
    }

    /**
     * 替换字符串
     *
     * @param from   String 原始字符串
     * @param to     String 目标字符串
     * @param source String 母字符串
     * @return String 替换后的字符串
     */
    public static String replace(String from, String to, String source) {
        if (Objects.isNull(source) || Objects.isNull(from) || Objects.isNull(to)) {
            return null;
        }

        StringBuilder str = new StringBuilder();
        int index;
        while ((index = source.indexOf(from)) != -1) {
            str.append(source, 0, index).append(to);
            source = source.substring(index + from.length());
        }
        str.append(source);
        return str.toString();
    }

    /**
     * 数组转换成String, 默认分隔符为,
     *
     * @param objects 数组
     */
    public static String toString(Object[] objects) {
        if (Objects.isNull(objects)) return null;
        if (objects.length == 0) return "";

        StringBuilder sb = new StringBuilder();
        for (Object obj : objects) {
            sb.append(',').append(ObjectKit.toString(obj));
        }

        return sb.substring(1);
    }

    /**
     * 数组转换成String
     *
     * @param objects 数组
     * @param split   分隔符
     */
    public static String toString(Object[] objects, String split) {
        if (Objects.isNull(objects)) return null;
        if (objects.length == 0) return "";

        StringBuilder sb = new StringBuilder();
        for (Object obj : objects) {
            sb.append(split).append(ObjectKit.toString(obj));
        }

        return sb.substring(1);
    }

    /**
     * 集合转换成String, 默认分隔符为,
     *
     * @param collection 集合
     */
    public static String toString(Collection collection) {
        return toString(collection, ",");
    }

    /**
     * 集合转换成String
     *
     * @param collection 集合
     * @param split      分隔符
     */
    public static String toString(Collection collection, String split) {
        if (Objects.isNull(collection)) {
            return null;
        } else if (collection.size() == 0) {
            return "";
        } else {
            StringBuilder sb = new StringBuilder();
            for (Object obj : collection) {
                sb.append(split).append(obj.toString());
            }
            return sb.substring(1);
        }
    }

    public static String toQuoteString(Collection collection) {
        if (Objects.isNull(collection)) {
            return null;
        } else if (collection.size() == 0) {
            return "";
        } else {
            StringBuilder sb = new StringBuilder();
            for (Object obj : collection) {
                sb.append(",").append("\"").append(obj.toString()).append("\"");
            }
            return sb.substring(1);
        }
    }

    /**
     * 快捷替换
     * 源字符串 "insert into {0} values({1},{2})"
     * 传入参数为 test、id、name
     * 替换后的字符串为 "insert into test value(id,name)"
     *
     * @param str  源字符串
     * @param args 参数
     * @return 替换后的字符串
     */
    public static String replaceRegex(String str, Object... args) {
        if (Objects.isNull(args)) return str;

        for (int i = 0; i < args.length; i++) {
            String arg = args[i].toString();
            str = str.replace("{" + i + "}", arg);
        }

        return str;
    }

    /**
     * 有分隔符的字符串根据分隔符首字母大写,其余字母小写
     * hello-World -> Hello-World
     */
    public static String firstUpperCase(String str, String split) {
        return StringUtils.hasText(str) ?
                Stream.of(str.split(split, -1))
                        .map(val -> val.substring(0, 1)
                                .toUpperCase()
                                .concat(val.substring(1).toLowerCase()))
                        .collect(Collectors.joining(split)) : "";
    }

    /**
     * 首字母大写
     */
    public static String firstUpperCase(String str) {
        return StringUtils.hasText(str) ? (str.length() == 1 ? str.toUpperCase() : str.substring(0, 1).toUpperCase() + str.substring(1)) : "";
    }

    /**
     * 首字母小写
     */
    public static String firstLowerCase(String str) {
        return StringUtils.hasText(str) ? (str.length() == 1 ? str.toLowerCase() : str.substring(0, 1).toLowerCase() + str.substring(1)) : "";
    }

    public static String append(String... strings) {
        StringBuilder sb = new StringBuilder();
        for (String s : strings) {
            sb.append(s);
        }
        return sb.toString();
    }

    public static String at(String str, int pos) {
        return pos >= str.length() ? null : str.substring(pos, pos + 1);
    }

    /**
     * 按行读取字符串转换为Set集合
     *
     * @param str 源字符串
     * @return Set集合
     */
    public static Set readerLine(String str) {
        Set strings = Sets.newHashSet();
        if (StringUtils.hasText(str)) {
            LineNumberReader lineNumberReader = new LineNumberReader(new StringReader(str));
            String line;
            try {
                while (Objects.nonNull(line = lineNumberReader.readLine())) {
                    strings.add(line);
                }
            } catch (IOException e) {
                log.error(e.getMessage());
            }
            strings.removeIf(StringUtils::isEmpty);
        }
        return strings;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy