[企业应用]一个实用的去除前后空格的小工具
企业开发中往往有这样的需求,用户界面输入的东西都要去除前后空格。
之前的做法是,取出formbean里的每一个属性依次验证。想想,费时又费用,抽时间我写了一个通用工具类,一句代码就能搞定。
用法如下:
FormBean:
public class FormBean { private int age; private String name; private Date birth; private String[] fav; private int score[]; public int[] getScore() { return score; } public void setScore(int[] score) { this.score = score; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } public String[] getFav() { return fav; } public void setFav(String[] fav) { this.fav = fav; } public String getName() { return name; } public void setName(String name) { this.name = name; } } 测试:
FormBean f = new FormBean();f.setAge(232);f.setBirth(new Date());f.setName(" ddd "); f.setFav(new String[]{" aa "," bb "});f.setScore(new int[2]);FormBeanUtil.stripStringProperty(f); //去前后空格 工具代码: