package com.moral.common.util;
|
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.Date;
|
|
public class StringUtils {
|
public static final char UNDERLINE = '_';
|
|
/**
|
* 驼峰格式字符串转换为下划线格式字符串
|
*
|
* @param param
|
* @return
|
*/
|
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 s
|
* @return
|
*/
|
public static String toUpperCaseFirstOne(String s){
|
StringBuilder sb = new StringBuilder(s);
|
sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
|
return sb.toString();
|
}
|
public static boolean isNullOrEmpty(String toTest) {
|
return toTest == null || toTest.length() == 0;
|
}
|
public static Object stringToObject4Type(Class<?> type,String value) throws Exception{
|
Object result = value;
|
if (type == double.class || type == Double.class) {
|
result = Double.parseDouble(value);
|
} else if (type == float.class || type == Float.class) {
|
result = Float.parseFloat(value);
|
} else if (type == long.class || type == Long.class) {
|
result = Long.parseLong(value);
|
} else if (type == int.class || type == Integer.class) {
|
result = Integer.parseInt(value);
|
} else if (type == short.class || type == Short.class) {
|
result = Short.parseShort(value);
|
} else if (type == boolean.class || type == Boolean.class) {
|
result = Boolean.parseBoolean(value);
|
} else if (type == Date.class) {
|
value = value.replace("T", " ");
|
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
try {
|
result = df.parse(value);
|
} catch (Exception e) {
|
df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
try {
|
result = df.parse(value);
|
} catch (ParseException e1) {
|
df = new SimpleDateFormat("yyyy-MM-dd");
|
try {
|
result = df.parse(value);
|
} catch (ParseException e2) {
|
}
|
}
|
}
|
}
|
return result;
|
}
|
|
/**
|
* 中英文字符长度控制
|
* @param str
|
* @param maxLength
|
* @return
|
*/
|
public static String subStringCN(final String str, final int maxLength) {
|
if (str == null) {
|
return str;
|
}
|
String suffix = "...";
|
int suffixLen = suffix.length();
|
|
final StringBuffer sbuffer = new StringBuffer();
|
final char[] chr = str.trim().toCharArray();
|
int len = 0;
|
for (int i = 0; i < chr.length; i++) {
|
|
if (chr[i] >= 0xa1) {
|
len += 2;
|
} else {
|
len++;
|
}
|
}
|
|
if(len<=maxLength){
|
return str;
|
}
|
|
len = 0;
|
for (int i = 0; i < chr.length; i++) {
|
|
if (chr[i] >= 0xa1) {
|
len += 2;
|
if (len + suffixLen > maxLength) {
|
break;
|
}else {
|
sbuffer.append(chr[i]);
|
}
|
} else {
|
len++;
|
if (len + suffixLen > maxLength) {
|
break;
|
}else {
|
sbuffer.append(chr[i]);
|
}
|
}
|
}
|
sbuffer.append(suffix);
|
return sbuffer.toString();
|
}
|
public static boolean isNumericZidai(String str) {
|
for (int i = 0; i < str.length(); i++) {
|
System.out.println(str.charAt(i));
|
if (!Character.isDigit(str.charAt(i))) {
|
return false;
|
}
|
}
|
return true;
|
}
|
}
|