java 字符串大小写操作

来源:互联网 发布:沈阳seo公司 编辑:程序博客网 时间:2024/06/07 02:20

java在操作字符串的首字母变为大写,以及list根据list中对象的某字段(为String类型),去重,相关代码如下

      /**     * 对按fieldName分组的list<object>进行fieldName字段去重复的操作,其中fieldName在object中卫String类型     * @param list     * @param fieldName 字段名(该字段类型为String)     * @return     * @throws Exception     */@SuppressWarnings("unchecked")public static <T> List<T> removeRepeatItemByField(List<T> list, String fieldName) throws Exception{        //如果传入参数无效,返回list    if (fieldName.trim().length() == 0 || list == null ||list.size() == 0) {            return list;        }        Class<T> clazz = (Class<T>) list.get(0).getClass();        if (clazz == null ) {log.info("找不到映射对象");        return list;}        Field field = clazz.getDeclaredField(fieldName);        //获取get、set方法        PropertyDescriptor descriptor = new PropertyDescriptor(fieldName, clazz);        Method readMethod = descriptor.getReadMethod();        Method writeMethod = descriptor.getWriteMethod();        //如果属性存在list中对象中,且包含该属性的读写方法        if (field != null && readMethod != null && writeMethod != null) {            List<String> ls = new ArrayList<String>();            for (int i = 0; i < list.size(); i++) {                Object obj = list.get(i);                String s = (String) readMethod.invoke(obj);                if (ls.contains(s)) {                    writeMethod.invoke(obj, "");                }else {                    ls.add(s);                }            }        } else {log.info("在对象列表中,对象无" + fieldName + "字段或者该属性的读写方法不完整");}        return list;    }        /**     * 字符串首字母大写     * @param name     * @return     */    public static String captureName(String name) {    if (name == null || name.trim().length() == 0) {return name;}        char[] cs = name.toCharArray();        //cs[0] -= (cs[0] > 96 && cs[0] < 123)?32:0;        if (97 <= cs[0] && cs[0] <= 122) {          cs[0] -= 32;        }        return String.valueOf(cs);    }


0 0
原创粉丝点击