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

com.cntool.core.string.StrUtils Maven / Gradle / Ivy

The newest version!
package com.cntool.core.string;

import com.cntool.core.component.NumberPool;
import com.cntool.core.component.StringPool;
import com.cntool.core.component.SymbolPool;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.compile;

/**
 * @program: cntool
 * @description: 字符串工具类
 * @author: ID-Tang
 * @create: 2022-01-21 10:12
 * @copyright: Copyright (c) [2022] [ID-tang]
 * [cntool] is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 * http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 **/
public class StrUtils {
    /**
     * 判断字符串是否是整数
     */
    public static boolean isInteger(String value) {
        try {
            Integer.parseInt(value);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    /**
     * 判断字符串是否是long类型
     */
    public static boolean isLong(String value) {
        try {
            Long.valueOf(value);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    /**
     * 验证手机号格式是否正确
     *
     * @param phone 手机号
     * @return true为正确 false为不正确
     */
    public static boolean isPhone(String phone) {
        Pattern p = compile(StringPool.PHONE_FORMAT);
        Matcher m = p.matcher(phone);
        return m.matches();
    }

    /**
     * 判断字符串中是否全是空白字符
     *
     * @param cs 需要判断的字符串
     * @return 如果字符串序列是 null 或者全是空白,返回 true
     */
    public static boolean isBlank(CharSequence cs) {
        if (cs != null) {
            int length = cs.length();
            for (int i = 0; i < length; i++) {
                if (!Character.isWhitespace(cs.charAt(i))) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 判断字符串中是否为非全空白字符
     *
     * @param cs 需要判断的字符串
     * @return 如果字符串序列是 null 或者全是空白,返回 true
     */
    public static boolean isNotBlank(CharSequence cs) {
        return !isBlank(cs);
    }

    /**
     * 判断字符串是否是全英文
     * 判断的方法为str.matches("^[a-zA-Z]*")
     * 但是当字符串中包含空格时
     * 判断的结果会为false
     * 本方法会先将字符串的所有空格去掉后再进行判断
     *
     * @param str 字符串
     * @return true全英文 false非全英文
     */
    public static boolean isEnglish(String str) {
        return str.replaceAll(" ", "").matches(SymbolPool.ENGLISH);
    }

    /**
     * 驼峰转下划线小写
     *
     * @param str 转换前的驼峰式命名的字符串
     * @return 转换后下划线小写方式命名的字符串
     */
    private static String humpToUnderline(String str) {
        if (StrUtils.isBlank(str)) {
            return str;
        }
        StringBuilder result = new StringBuilder();
        // 循环处理字符
        for (int i = 0; i < str.length(); i++) {
            String s = str.substring(i, i + NumberPool.ONE);
            // 在大写字母前添加下划线
            if (s.equals(s.toUpperCase()) && !Character.isDigit(s.charAt(NumberPool.ZERO))) {
                result.append("_");
            }
            //字符串转成小写
            result.append(s.toLowerCase());
        }
        return result.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy