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);
}
}
其他
END。