博主首页

去掉前后空格、去掉属性前后空格、去空格

package com.yirui.supervisor.util;

import com.yirui.supervisor.vo.UserVO;

import java.lang.reflect.Field;

/**
 * 对象属性值处理
 */
public class DataTrim {

    public static void main(String[] args) {
        UserVO vo = new UserVO();
        vo.setUserid(" 123 ");
        trim(vo);
        System.out.println(vo);
    }

    /**
     * 去掉对象属性值前后空格
     * @param t
     * @param <T>
     * @return
     */
    public static <T> T trim(T t){
          try {
              for(Field f : t.getClass().getDeclaredFields()){
                  f.setAccessible(true);
                  Object o1 = f.get(t);
                  if(o1!=null){
                      f.set(t,o1.toString().trim());
                  }
              }
          }catch (Exception e){
              return t;
          }
        return t;
    }
}

 去掉所有空格,包含空白,换行等等、、、

.replaceAll("\\s*", "")

 

posted @ 2020-08-19 17:12  笑~笑  阅读(349)  评论(0编辑  收藏  举报