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

com.github.hetianyi.common.util.StringUtil Maven / Gradle / Ivy

package com.github.hetianyi.common.util;

import java.util.Objects;

/**
 * String通用操作工具类
 *
 * @author Jason He
 * @version 1.0.6
 * @since 1.0.0
 * @date 2020-01-18
 */
public class StringUtil {

    /**
     * 判断两个字符串是否相等
     * @param cs1
     * @param cs2
     * @return
     */
    public static final boolean equals(String cs1, String cs2) {
        return Objects.equals(cs1, cs2);
    }
    /**
     * 判断String是否为空
     * @param input
     * @return
     */
    public static final boolean isNullOrEmpty(String input) {
        return null == input || "".equals(input);
    }

    /**
     * 对String进行trim之后判断是否为空
     * @param input
     * @return
     */
    public static final boolean isNullOrEmptyTrimed(String input) {
        return null == input || "".equals(input.trim());
    }

    /**
     * 对String进行trim操作,可避免NPE
     * @param input
     * @return
     */
    public static final String trim(String input) {
        return null == input ? null : input.trim();
    }

    /**
     * 对String进行trim操作,如果输出是null,返回空字符串,可避免NPE
     * @param input
     * @return
     */
    public static final String trimSafe(String input) {
        return null == input ? "" : input.trim();
    }

    /**
     * 连接多个字符串
     * @param s
     * @return
     */
    public static final String join(String... s) {
        if (null == s || s.length == 0) {
            return null;
        }
        if (s.length == 1) {
            return s[0];
        }
        StringBuilder sb = new StringBuilder();
        for (String i : s) {
            sb.append(i);
        }
        return sb.toString();
    }

    /**
     * 连接多个字符串,并自定义分隔字符串
     * @param separator
     * @param s
     * @return
     */
    public static final String joinWithSeparator(String separator, String... s) {
        if (null == s || s.length == 0) {
            return null;
        }
        if (s.length == 1) {
            return s[0];
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0, len = s.length; i < len; i++) {
            sb.append(s[i]);
            if (i != len - 1) {
                sb.append(separator);
            }
        }
        return sb.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy