com.kukababy.plus.utils.StringUtil Maven / Gradle / Ivy
/**
*
*/
package com.kukababy.plus.utils;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import com.alibaba.fastjson.JSON;
/**
*
*
*
* 描述:
*
*
*
*
* @author [email protected]
* @date 2019年3月5日 下午10:48:56
*/
public class StringUtil {
public static final char UNDERLINE_CHAR = '_';
public static String getTableName(String className) {
return className.substring(0, 1).toLowerCase() + className.substring(1);
}
/**
*
*
*
*
* 描述:
* 驼峰转下划线
*
*
*
*
* @param camelStr
* @return
*/
public static String camel2Underline(String camelStr) {
if (StringUtils.isEmpty(camelStr)) {
return StringUtils.EMPTY;
}
int len = camelStr.length();
StringBuilder strb = new StringBuilder(len + len >> 1);
for (int i = 0; i < len; i++) {
char c = camelStr.charAt(i);
if (Character.isUpperCase(c)) {
strb.append(UNDERLINE_CHAR);
strb.append(Character.toLowerCase(c));
} else {
strb.append(c);
}
}
return strb.toString();
}
/**
*
*
*
*
* 描述:
* 下划线转驼峰
*
*
*
*
* @param underlineStr
* @return
*/
public static String underline2Camel(String underlineStr) {
if (StringUtils.isEmpty(underlineStr)) {
return StringUtils.EMPTY;
}
int len = underlineStr.length();
StringBuilder strb = new StringBuilder(len);
for (int i = 0; i < len; i++) {
char c = underlineStr.charAt(i);
if (c == UNDERLINE_CHAR && (++i) < len) {
c = underlineStr.charAt(i);
strb.append(Character.toUpperCase(c));
} else {
strb.append(c);
}
}
return strb.toString();
}
public static String select2Lower(String sql) {
// String sql = "seLect * from SelEct";
return sql.replaceAll("[sS][eE][lL][eE][cC][tT]", "select");
}
public static String from2Lower(String sql) {
return sql.replaceAll("[fF][rR][oO][mM]", "from");
}
/**
*
*
*
*
* 描述:
* 构建sql语句的统计
*
*
*
*
* @param sql
* @return
*/
public static String getCountSql(String sql) {
// sql = select2Lower(sql);
sql = from2Lower(sql);
int fromPos = sql.indexOf("from");
String countSql = "select count(1) from " + sql.substring(fromPos + 4);
return countSql;
}
public static Map humpConvert(Map rowMap) {
Map resMap = new HashMap();
for (Map.Entry entry : rowMap.entrySet()) {
String key = entry.getKey();
resMap.put(underline2Camel(key), entry.getValue());
}
return resMap;
}
public static void main(String args[]) {
Map map = new HashMap();
map.put("emp_id", "001");
map.put("agE_Id", "001");
map.put("addr_id", "001");
map.put("addTT_d", "001");
System.out.println(JSON.toJSONString(humpConvert(map)));
// String sql = "select we,wwe,ee from where From and from";
// System.out.println(getCountSql(sql));
}
}