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

org.yes.tools.generator.utils.StringUtils Maven / Gradle / Ivy

There is a newer version: 2.0.4
Show newest version
package org.yes.tools.generator.utils;

import cn.hutool.core.util.StrUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils extends org.apache.commons.lang3.StringUtils {

    /**
     * 下划线转驼峰法(默认小驼峰)
     *
     * @param line       源字符串
     * @param smallCamel 大小驼峰,是否为小驼峰(驼峰,第一个字符是大写还是小写)
     * @return 转换后的字符串
     */
    public static String underline2Camel(String line, boolean... smallCamel) {
        if (isBlank(line)) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        Pattern pattern = Pattern.compile("([A-Za-z\\d]+)(_)?");
        Matcher matcher = pattern.matcher(line);
        // 匹配正则表达式
        while (matcher.find()) {
            String word = matcher.group();
            // 当是true 或则是空的情况
            if ((smallCamel.length == 0 || smallCamel[0]) && matcher.start() == 0) {
                sb.append(Character.toLowerCase(word.charAt(0)));
            } else {
                sb.append(Character.toUpperCase(word.charAt(0)));
            }

            int index = word.lastIndexOf('_');
            if (index > 0) {
                sb.append(word.substring(1, index).toLowerCase());
            } else {
                sb.append(word.substring(1).toLowerCase());
            }
        }
        return sb.toString();
    }

    /**
     * 驼峰法转下划线
     *
     * @param line 源字符串
     * @return 转换后的字符串
     */
    public static String camel2Underline(String line) {
        if (isBlank(line)) {
            return "";
        }
        line = String.valueOf(line.charAt(0)).toUpperCase().concat(line.substring(1));
        StringBuffer sb = new StringBuffer();
        Pattern pattern = Pattern.compile("[A-Z]([a-z\\d]+)?");
        Matcher matcher = pattern.matcher(line);
        while (matcher.find()) {
            String word = matcher.group();
            sb.append(word.toUpperCase());
            sb.append(matcher.end() == line.length() ? "" : "_");
        }
        return sb.toString();
    }

    /**
     * 字符串URLEncoder
     *
     * @param str 字符串
     * @return URLEncoder结果
     */
    public static String utf8encode(String str) {
        if (StrUtil.isNotBlank(str)) {
            try {
                return URLEncoder.encode(str, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return StrUtil.EMPTY;
    }

    /***
     * 首字母大写
     *
     * @author Co.
     * @date: 2022/11/22 15:01
     * @return
     */
    public static String upperCase(String str) {
        char[] ch = str.toCharArray();
        if (ch[0] >= 'a' && ch[0] <= 'z') {
            ch[0] = (char) (ch[0] - 32);
        }
        return new String(ch);
    }

    /**
     * sql类型装换 ts
     * @param sqlType
     * @return
     */
    public static String sqlType2TsType(String sqlType) {
        switch (sqlType.toLowerCase()){
            case "bit": return "number";
            case "tinyint": return "number";
            case "smallint": return "number";
            case "int": return "number";
            case "bigint": return "string";
            case "float": return "string";
            case "decimal": return "string";
            case "numeric": return "string";
            case "real": return "string";
            case "money": return "string";
            case "smallmoney": return "number";
            case "varchar": return "string";
            case "char": return "string";
            case "nvarchar": return "string";
            case "nchar":return "string";
            case "text": return "String";
            case "datetime": return "string";
            case "date": return "string";
            default: return "String";
        }
    }

    public static void main(String[] args) {
        String line = "are_you_ok";
        // 下划线转驼峰(大驼峰)
        // AreYouOk
        String camel = underline2Camel(line, false);
        System.out.println(camel);

        // 下划线转驼峰(小驼峰)
        // areYouOk
        camel = underline2Camel(line);
        System.out.println(camel);

        // 驼峰转下划线
        // ARE_YOU_OK
        System.out.println(camel2Underline(camel));

        System.out.println(sqlType2TsType("DATETIME"));
        //去掉前缀
        String s = line.replaceFirst(line.split("_")[0] + "_", "");
        // 下划线转驼峰(小驼峰)
        String s1 = underline2Camel(s);

        String sss = underline2Camel(s, false);
        System.out.println(sss);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy