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

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

The newest version!
package cq.web.utils;

import java.lang.reflect.InvocationTargetException;

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 String addFirstOblihne(String s) {
        if (s.charAt(0) == '/') {
            return s;
        } else {
            return "/" + s;
        }
    }

    public static Object str2obj(String s, Class clazz) {
        if (s == null || s.length() == 0) {
            return null;
        }
        if (clazz == int.class) {
            clazz = Integer.class;
        } else if (clazz == double.class) {
            clazz = Double.class;
        } else if (clazz == float.class) {
            clazz = Float.class;
        } else if (clazz == char.class) {
            clazz = Character.class;
        } else if (clazz == byte.class) {
            clazz = Byte.class;
        } else if (clazz == long.class) {
            clazz = Long.class;
        } else if (clazz == boolean.class) {
            clazz = Boolean.class;
        } else if (clazz == short.class) {
            clazz = Short.class;
        }
        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;
            }
        }
        return s;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy