cq.json.util.StringUtil Maven / Gradle / Ivy
The newest version!
package cq.json.util;
import java.lang.reflect.InvocationTargetException;
import java.util.regex.Pattern;
public class StringUtil {
/**
* 字符串首字母转大写
*/
public static String first2Upper(String s) {
if (Character.isUpperCase(s.charAt(0))) {
return s;
} else {
return Character.toUpperCase(s.charAt(0)) + s.substring(1);
}
}
public static Object str2obj(String s, Class> clazz) {
if (s == null || s.length() == 0) {
return null;
}
clazz = ClassUtil.enca(clazz);
if (Number.class.isAssignableFrom(clazz) || clazz == Boolean.class) {
try {
return clazz.getConstructor(String.class).newInstance(s);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException | SecurityException e) {
e.printStackTrace();
return null;
}
}
if (clazz == Character.class){
return s.charAt(0);
}
return s;
}
public static boolean noString(String s) {
return Pattern.matches("^(-?\\d+)(\\.\\d+)?$", s) || s.equalsIgnoreCase("true") || s.equalsIgnoreCase("false") || s.equalsIgnoreCase("null");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy