cn.sixlab.sixutil.StrUtil.java Source code

Java tutorial

Introduction

Here is the source code for cn.sixlab.sixutil.StrUtil.java

Source

/*
 * Copyright (c) 1995 Sixlab. All rights reserved.
 *
 * Under the GPLv3(AKA GNU GENERAL PUBLIC LICENSE Version 3).
 * see http://www.gnu.org/licenses/gpl-3.0-standalone.html
 *
 * For more information, please see
 * http://sixlab.cn/
 */
package cn.sixlab.sixutil;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

/**
 * @author /loki
 * @since 1.0.0
 */
public class StrUtil {

    //genStrFromTemp() ?
    private static final String STR_TEMP_SEP = "$";

    //
    private static final Integer INDEX_NOT_FOUND = -1;

    /**
     * ??
     *
     * @param str ?
     * @return {@code str}?true {@code str}false
     */
    public static Boolean isNotNumber(String str) {
        return !NumberUtils.isNumber(str);
    }

    /**
     * ?
     *
     * @param str ?
     * @return {@code str}true {@code str}?false
     */
    public static Boolean isPositiveIntegralNumber(String str) {
        try {
            Long value = Long.valueOf(str);
            if (value > 0) {
                return true;
            }
            return false;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * ??
     *
     * @param str ?
     * @return {@code str}false {@code str}?true
     */
    public static Boolean isNotPositiveIntegralNumber(String str) {
        try {
            Long value = Long.valueOf(str);
            if (value > 0) {
                return false;
            }
            return true;
        } catch (Exception e) {
            return true;
        }
    }

    /**
     * ?
     *
     * @param str ?
     * @return {@code str}true {@code str}?false
     */
    public static Boolean isNaturalNumber(String str) {
        try {
            long value = Long.valueOf(str);
            if (value >= 0) {
                return true;
            }
            return false;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * ??
     *
     * @param str ?
     * @return {@code str}true {@code str}?false
     */
    public static Boolean isNotNaturalNumber(String str) {
        try {
            long value = Long.valueOf(str);
            if (value >= 0) {
                return false;
            }
            return true;
        } catch (Exception e) {
            return true;
        }
    }

    /**
     * ??
     *
     * @param str ?
     * @return {@code str}?true {@code str}??false
     */
    public static Boolean isEmail(String str) {
        String regex = "w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*";
        return str.matches(regex);
    }

    /**
     * ?null?null ?nullnull
     * null??
     *
     * @param str ?
     * @return {@code str}?null {@code str}null
     * @see #notNullStr(java.lang.String, java.lang.String)
     */
    public static String notNullStr(String str) {
        return null == str ? "" : str;
    }

    /**
     * ?null?null ?nullnull{@code defaultStr}
     * {@code defaultStr}????
     *
     * @param str ?
     * @param defaultStr null
     * @return {@code str}?null {@code str}null{@code
     * defaultStr}
     * @see #notNullStr(java.lang.String)
     */
    public static String notNullStr(String str, String defaultStr) {
        return null == str ? (null == defaultStr ? "" : defaultStr) : str;
    }

    /**
     * ????
     *
     * @param ulStr ??
     * @return ????
     * @since 1.0.0
     */
    public static String getCamel(String ulStr) {
        return getCamel(ulStr, true);
    }

    /**
     * ???????
     *
     * @param ulStr ??
     * @param isFirstLower ???
     * @return ??@{code isFirstLower}true????
     * @since 1.0.0
     */
    public static String getCamel(String ulStr, boolean isFirstLower) {
        StringBuffer result = new StringBuffer();

        String[] ulArray = ulStr.split("_");

        boolean isFirst = true;
        for (String s : ulArray) {
            if (StringUtils.isNotEmpty(s)) {
                s = StringUtils.lowerCase(s);
                if (isFirst && isFirstLower) {
                    isFirst = false;
                    result.append(s);
                } else {
                    result.append(StringUtils.swapCase(s.substring(0, 1))).append(s.substring(1, s.length()));
                }
            }
        }

        return result.toString();
    }

}