com.github.wendao76.util.StringHelper Maven / Gradle / Ivy
The newest version!
package com.github.wendao76.util;
import com.alibaba.druid.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import java.lang.reflect.Field;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* @author wendao76 .
* @version 1.0.0 .
*/
@Slf4j
public class StringHelper {
/**
* Parse string string.
*
* @param strList the str list
* @param splitStr the split str
* @return the string
*/
public static String toStr(List strList, String splitStr){
StringBuffer stringBuffer = new StringBuffer();
for(String str : strList){
stringBuffer.append(str + splitStr);
}
return stringBuffer.substring(0, stringBuffer.length()-1);
}
/**
* To str string.
*
* @param the type parameter
* @param objList the obj list
* @param field the field
* @param splitStr the split str
* @return the string
*/
public static String toStr(List objList, String field, String splitStr){
StringBuilder stringBuffer = new StringBuilder();
for(T obj:objList){
try {
Field fld = obj.getClass().getDeclaredField(field);
fld.setAccessible(true);
stringBuffer.append(",").append(fld.get(obj));
} catch (NoSuchFieldException e) {
log.error(e.getMessage(), e);
return "";
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return stringBuffer.substring(1);
}
/**
* The constant UNDERLINE.
*/
// 定义区分驼峰标志
public static final char UNDERLINE='_';
/**
* 驼峰转通过标志分隔字符串
*
* @param param the param
* @return string
*/
public static String camelToUnderline(String param){
if (param==null||"".equals(param.trim())){
return "";
}
int len=param.length();
StringBuilder sb=new StringBuilder(len);
for (int i = 0; i < len; i++) {
char c=param.charAt(i);
if (Character.isUpperCase(c)){
sb.append(UNDERLINE);
sb.append(Character.toLowerCase(c));
}else{
sb.append(c);
}
}
return sb.toString();
}
/**
* 定义标志转驼峰
*
* @param param the param
* @return string
*/
public static String underlineToCamel(String param){
if (param==null||"".equals(param.trim())){
return "";
}
StringBuilder sb=new StringBuilder(param);
Matcher mc= Pattern.compile("_").matcher(param);
int i=0;
while (mc.find()){
int position=mc.end()-(i++);
sb.replace(position-1,position+1,sb.substring(position,position+1).toUpperCase());
}
return sb.toString();
}
/**
* 定义标志转驼峰
*
* @param param the param
* @param firstCharParse the param
* @return string
*/
public static String underlineToCamel(String param, boolean firstCharParse){
if(StringUtils.isEmpty(param)){
return "";
}
if(firstCharParse){
param = param.substring(0,1).toUpperCase() + param.substring(1);
}
return underlineToCamel(param);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy