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

com.gitee.feizns.StringUtils Maven / Gradle / Ivy

There is a newer version: 5.5-RELEASE
Show newest version
package com.gitee.feizns;

import com.gitee.feizns.convert.ConvertUtils;
import lombok.NonNull;

import java.util.Arrays;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.IntStream;

/**
 * 字符串
 * @author feizns
 * @since 2019/6/9 0009
 */
public abstract class StringUtils {

    /**
     * 空白字符
     */
    public static final Character BLANK_CHARACTER = ' ';

    /**
     *
     */
    public static final String EMPTY_STRING = "";

    /**
     * 首字母大写
     * @param original
     * @return
     */
    public static final String firstUpper(String original) {
        return isEmpty(original) ? original : Character.toUpperCase(original.charAt(0)) + original.substring(1);
    }

    /**
     * 首字母小写
     * @param original
     * @return
     */
    public static final String firstLower(String original) {
        return isEmpty(original) ? original : Character.toLowerCase(original.charAt(0)) + original.substring(1);
    }

    /**
     * 是否为空
     * @param str
     * @return
     */
    public static final boolean isEmpty(String str) {
        return str == null || str.isEmpty();
    }

    /**
     * 全空白
     * @param str
     * @return
     */
    public static final boolean isEmptyOrBlank(String str) {
        return isEmpty(str) || str.trim().isEmpty();
    }

    /**
     * 使用逗号分隔实现,数据转换
     * @param str
     * @return
     */
    public static int[] toIntArray(@NonNull String str) {
        return toIntStream(str).toArray();
    }

    /**
     * 使用逗号分隔
     * @param str
     * @return
     */
    public static IntStream toIntStream(@NonNull String str) {
        return toIntStream(",", str);
    }

    /**
     * 使用逗号分隔
     * @param str
     * @return
     */
    public static IntStream toIntStream(String delimiter, @NonNull String str) {
        String[] split = str.split(delimiter);
        return Arrays.stream(split)
                .map(original -> ConvertUtils.to(original, Integer.class))
                .filter(Objects::nonNull)
                .mapToInt(Integer::intValue);
    }

    /**
     * 消费字符
     * @param str
     * @param consumer
     */
    public static void consumerStr(String str, Consumer consumer) {
        StreamUtils.stream(str).forEach(consumer);
    }

    /**
     * 消费字符
     * @param str
     * @param consumer
     */
    public static void consumerStr(String str, BiConsumer consumer) {
        if ( str != null ) {
            for (int i = 0; i < str.length(); i++) {
                consumer.accept(str.charAt(i), i);
            }
        }
    }

    /**
     * 转下划线 (适用于帕斯卡和驼峰)
     * firstName => firstName
     * FirstName => firstName
     * @param name
     * @return
     */
    public static String toUnderline(String name) {
        StringBuilder ret = new StringBuilder();
        int last = 0;
        for (int i = 0; i < name.length(); i++) {
            if ( Character.isUpperCase(name.charAt(i)) ) {
                ret.append(firstLower(name.substring(last, i)));
                if ( ret.length() != 0 )
                    ret.append("_");
                last = i;
            }
        }
        ret.append(firstLower(name.substring(last)));
        return ret.toString();
    }

    /**
     * 下划线转 Pascal
     * @param underlineName
     * @return
     */
    public static String underlineToPascal(String underlineName) {
        StringBuilder ret = new StringBuilder();
        int last = 0;
        for (int i = 0; i < underlineName.length() && last < underlineName.length(); i++) {
            if ( underlineName.charAt(i) == '_' ) {
                ret.append(firstUpper(underlineName.substring(last, i)));
                last = i + 1;
            }
        }
        ret.append(firstUpper(underlineName.substring(last)));
        return ret.toString();
    }

    /**
     * 下划线转驼峰
     * @param underlineName
     * @return
     */
    public static String underlineToHump(String underlineName) {
        return firstLower(underlineToPascal(underlineName));
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy