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

fun.langel.cql.util.StringUtil Maven / Gradle / Ivy

The newest version!
package fun.langel.cql.util;

/**
 * @author [email protected](GuHan)
 * @since 2022/3/21 20:11
 **/
public class StringUtil {

    private StringUtil() {
    }

    public static String withDefault(String value, String def) {
        return value == null || value.isEmpty() ? def : value;
    }

    public static String wrap(final Object v) {
        return v == null ? null : v.toString();
    }

    public static String trim(final String v) {
        return v == null ? null : v.trim();
    }


    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }


    public static String strip(String str, String stripChars) {
        if (isEmpty(str)) {
            return str;
        } else {
            str = stripStart(str, stripChars);
            return stripEnd(str, stripChars);
        }
    }

    public static String stripQuote(String v) {
        if (isEmpty(v)) {
            return v;
        }
        if ((v.startsWith("'") && v.endsWith("'"))
                || (v.startsWith("`") && v.endsWith("`"))
                || (v.startsWith("\"") && v.endsWith("\""))) {
            return v.substring(1, v.length() - 1);
        }
        return v;
    }


    public static String stripStart(String str, String stripChars) {
        int strLen;
        if (str != null && (strLen = str.length()) != 0) {
            int start = 0;
            if (stripChars == null) {
                while (start != strLen && Character.isWhitespace(str.charAt(start))) {
                    ++start;
                }
            } else {
                if (stripChars.isEmpty()) {
                    return str;
                }

                while (start != strLen && stripChars.indexOf(str.charAt(start)) != -1) {
                    ++start;
                }
            }

            return str.substring(start);
        } else {
            return str;
        }
    }

    public static String stripEnd(String str, String stripChars) {
        int end;
        if (str != null && (end = str.length()) != 0) {
            if (stripChars == null) {
                while (end != 0 && Character.isWhitespace(str.charAt(end - 1))) {
                    --end;
                }
            } else {
                if (stripChars.isEmpty()) {
                    return str;
                }

                while (end != 0 && stripChars.indexOf(str.charAt(end - 1)) != -1) {
                    --end;
                }
            }

            return str.substring(0, end);
        } else {
            return str;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy