All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cq.json.utils.StringUtils Maven / Gradle / Ivy

The newest version!
package cq.json.utils;

import java.lang.reflect.InvocationTargetException;
import java.util.regex.Pattern;

public class StringUtils {
    /**
     * 字符串首字母转大写
     */
    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 = ClassUtils.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