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

com.efeichong.util.StringUtils Maven / Gradle / Ivy

package com.efeichong.util;

import com.google.common.base.CaseFormat;
import com.google.common.base.Joiner;
import lombok.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.Iterator;

/**
 * @author lxk
 * @date 2020/10/21
 * @description 字符串处理工具类
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils {
    /**
     * 下划线转驼峰
     *
     * @param name
     * @return
     */
    public static String convertToHump(@NonNull String name) {
        if (name.indexOf("_") == -1) {
            return name;
        }
        return CaseFormat.LOWER_UNDERSCORE.converterTo(CaseFormat.LOWER_CAMEL).convert(name);
    }

    /**
     * 驼峰转下划线
     *
     * @param name
     * @return
     */
    public static String convertToLine(@NonNull String name) {
        return CaseFormat.LOWER_CAMEL.converterTo(CaseFormat.LOWER_UNDERSCORE).convert(name);
    }

    /**
     * 首字母转小写
     *
     * @param name
     * @return
     */
    public static String toLowerCaseFirstOne(String name) {
        if (Character.isLowerCase(name.charAt(0))) {
            return name;
        } else {
            return (new StringBuilder()).append(Character.toLowerCase(name.charAt(0))).append(name.substring(1)).toString();
        }
    }

    /**
     * 首字母转大写
     *
     * @param name
     * @return
     */
    public static String toUpperCaseFirstOne(String name) {
        if (Character.isUpperCase(name.charAt(0))) {
            return name;
        } else {
            return (new StringBuilder()).append(Character.toUpperCase(name.charAt(0))).append(name.substring(1)).toString();
        }
    }

    /**
     * 去掉字符串前后的指定字符
     *
     * @param str
     * @param splitter
     * @return
     */
    public static String trimBothChars(String str, String splitter) {
        String regex = "^" + splitter + "*|" + splitter + "*$";
        return str.replaceAll(regex, "");
    }

    public static String join(Iterable parts, String separator) {
        return Joiner.on(separator).join(parts);
    }

    public static String join(Iterator parts, String separator) {
        return Joiner.on(separator).join(parts);
    }

    public static String join(Object[] parts, String separator) {
        return Joiner.on(separator).join(parts);
    }

    public static String join(@Nullable Object first, @Nullable Object second, String separator, Object... rest) {
        return Joiner.on(separator).join(first, second, separator, rest);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy