Java 常用工具类(22) : 字符串处理

fastjson

<dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.83</version>
        </dependency>


import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

/**
 * @Auther: liyue
 * @Date: 2018/10/24 14:30
 * @Description: 字符串工具类
 */
public class StringUtils {

    public static void main(String[] args) {
        System.out.println(xiaHuaXianToTuoFeng("sdfs_sfs_sdf"));
    }

    public static void info(String str, Object... param) {
        System.out.println(join(str, param));
    }

    public static Boolean isNotBlank(String str) {
        return !isBlank(str);
    }

    public static Boolean isBlank(String str) {
        if (str == null || str.length() == 0) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }


    /**
     * 首字母转小写
     *
     * @param str
     * @return
     */
    public static String toLowerCaseFirstOne(String str) {
        if (Character.isLowerCase(str.charAt(0)))
            return str;
        else
            return (new StringBuilder()).append(Character.toLowerCase(str.charAt(0))).append(str.substring(1)).toString();
    }


    /**
     * 首字母转大写
     *
     * @param str
     * @return
     */
    public static String toUpperCaseFirstOne(String str) {
        if (Character.isUpperCase(str.charAt(0)))
            return str;
        else
            return (new StringBuilder()).append(Character.toUpperCase(str.charAt(0))).append(str.substring(1)).toString();
    }

    /**
     * json字符串参数名首字母转大/小写
     *
     * @param str
     * @return
     */
    public static String jsonParamNameFirstOneConvert(String str, Integer rule) {
        String splitRule0 = "", splitRule1 = ":", splitRule2 = ",\"", splitRule3 = "\"", splitRule4 = "{", splitRule5 = "[";
        String[] split = str.split(splitRule1);
        List<String> params = new LinkedList<>();
        for (int i = 0; i < split.length; i++) {
            if (i == (split.length - 1)) {
                break;
            }
            String[] ss = split[i].split(splitRule2);
            String result = ss[ss.length - 1].replace(splitRule3, splitRule0).replace(splitRule4, splitRule0).replace(splitRule5, splitRule0);
            params.add(result);
        }
        for (String param : params) {
            String newParam = null;
            if (rule.equals(1)) {
                newParam = toUpperCaseFirstOne(param);
            } else {
                newParam = toLowerCaseFirstOne(param);
            }
            str = str.replace(param, newParam);
        }
        return str;
    }

    /**
     * 根据正则提前匹配字符串
     *
     * @param regex
     * @param str
     * @return
     */
    public static String getMatcher(String regex, String str) {
        String result = "";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
            result = matcher.group(1);
        }
        return result;
    }


    /**
     * 大写字母转小写并在该字母前面加_
     *
     * @param name
     * @return
     */
    public static String bigLetterToSmallAndBeforeAdd_(String name) {
        StringBuffer stringBuffer = new StringBuffer();
        char[] chars = name.toCharArray();
        for (char ch : chars) {
            boolean digit = Character.isUpperCase(ch);
            if (digit) {
                String str = String.valueOf(ch);
                String lstr = str.toLowerCase();
                stringBuffer.append("_");
                stringBuffer.append(lstr);
            } else {
                stringBuffer.append(ch);
            }
        }
        return stringBuffer.toString();
    }

    /**
     * 驼峰转下划线
     *
     * @param str
     * @return
     */
    public static String tuoFengToxiaHuaXian(String str) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            if (Character.isUpperCase(str.charAt(i))) {
                sb.append("_").append(Character.toLowerCase(str.charAt(i)));
            } else {
                sb.append(str.charAt(i));
            }
        }
        return sb.toString();
    }

    /**
     * 下划线转驼峰
     *
     * @param str
     * @return
     */
    public static String xiaHuaXianToTuoFeng(String str) {
        String[] strings = str.split("_");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < strings.length; i++) {
            if (i == 0) {
                sb.append(strings[i]);
            } else {
                sb.append(strings[i].substring(0, 1).toUpperCase() + strings[i].substring(1));
            }
        }
        return sb.toString();
    }


    /**
     * 依次替换{}
     * 例 : join("a={},b={}",1,2) => a=1,b=2
     *
     * @param str
     * @param param
     * @return
     */
    public static String join(String str, Object... param) {
        try {
            for (Object p : param) {
                str = str.replaceFirst("\\{\\}", p.toString());
            }
        } catch (Exception e) {
            for (Object p : param) {
                str = str + p;
            }
        }
        return str;
    }

    /**
     * 对象转json格式化输出
     *
     * @param o
     * @return
     */
    public static String formatJson(Object o) {
        return JSON.toJSONString(o, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteDateUseDateFormat);
    }

}

其他 

    java中的字符,字符串,数字之间的转换

END。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值