前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >写一个去除实体参数中String类型值的空格和换行工具类

写一个去除实体参数中String类型值的空格和换行工具类

原创
作者头像
青衫染红尘
修改2021-11-29 10:13:55
2.4K0
修改2021-11-29 10:13:55
举报
文章被收录于专栏:Surpass' BlogSurpass' Blog

系统中数据经常会进行新增或者更新,正常情况下如实保存就行,特殊情况下则需要对传进来的参数进行一些特殊的处理,比如说去掉前后空格或者去掉换行或者中间的若干个空格,来使数据更加严谨和准确,排除掉烂数据。(还有一大部分原因就是测试的角度太刁钻)

所以经常会对每个参数进行单独处理,所以封装一个处理的工具类,简化数据处理过程。

坐标

代码语言:html
复制
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.15</version>
</dependency>

完整代码

代码语言:java
复制
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import java.util.*;

/**
 * @author Surpass
 * @Package com.develop
 * @Description: 处理参数内前后空格
 * @date 2021/11/27 10:00
 */
public class TrimStringUtil {

    /**
     * 替换Map中的value值并转换成 T , 默认全部处理
     * <p>Map<String, Object> map = new HashMap<>();</p>
     * <p>map.put("name", "    123456    ");</p>
     * <p>map.put("age", "    123");</p>
     * <p>map.put("address", "    北京    ");</p>
     * <p>Student student = TrimStringUtil.stringTrimDate(map, new TypeReference&lt;Student&gt;(){});</p>
     * @param hashMap           原始参数键值对
     * @param typeReference     转换类型
     * @return T
     * @throws
     * @author Surpass
     * @date 2021/11/27 10:18
     */
    public static <T> T stringTrimDate(Map<String, Object> hashMap, TypeReference<T> typeReference) {
        return stringTrimDate(hashMap, typeReference, false, "");
    }

    /**
     * 替换Map中的value值并转换成 T , 默认全部处理
     * <p>Map<String, Object> map = new HashMap<>();</p>
     * <p>map.put("name", "    123456    ");</p>
     * <p>map.put("age", "    123");</p>
     * <p>map.put("address", "    北京    ");</p>
     * <p>Student student = TrimStringUtil.stringTrimDate(map, new TypeReference&lt;Student&gt;(){}, true, "name", "age");</p>
     * @param hashMap           原始参数键值对
     * @param typeReference     转换类型
     * @return T
     * @throws
     * @author Surpass
     * @date 2021/11/27 10:18
     */
    public static <T> T stringTrimDate(Map<String, Object> hashMap, TypeReference<T> typeReference,
                                       boolean isInclude, String... keys) {
        return stringTrimDate(hashMap, typeReference, isInclude, Arrays.asList(keys));
    }

    /**
     * 替换Map中的value值并转换成 T ,根据isInclude判断需要处理的字段值
     * <p>Map<String, Object> map = new HashMap<>();</p>
     * <p>map.put("name", "    123456    ");</p>
     * <p>map.put("age", "    123");</p>
     * <p>map.put("address", "    北京    ");</p>
     * <p>Student student = TrimStringUtil.stringTrimDate(map, new TypeReference&lt;Student&gt;(){}, false, null);</p>
     * @param hashMap           原始参数键值对
     * @param typeReference     转换类型
     * @param isInclude         是否包含keys中的字段
     * @param keyList              字段枚举
     * @return T
     * @throws
     * @author Surpass
     * @date 2021/11/27 10:15
     */
    public static  <T> T stringTrimDate(Map<String, Object> hashMap, TypeReference<T> typeReference, boolean isInclude, List<String> keyList){
        if (keyList == null) {
            keyList = new ArrayList<String>(){{
                this.add("");
            }};
        }
        Set<Map.Entry<String, Object>> entries = hashMap.entrySet();
        for (Map.Entry<String, Object> entry : entries) {
            if (entry.getValue() != null){
                String key = entry.getKey();
                Object paramValue = entry.getValue();
                if (paramValue instanceof String){
                    String value = (String)paramValue;
                    if ((isInclude && keyList.contains(key)) || (!isInclude && !keyList.contains(key))) {
                        String dealString = value.trim().replaceAll("\r\n", "").replaceAll("\\s+", "");
                        entry.setValue(dealString);
                    }
                }
            }
        }
        return JSON.parseObject(JSONObject.toJSONString(hashMap), typeReference);
    }
}

测试类

代码语言:java
复制
public static void main(String[] args) throws Exception {
        TestTest testTest = new TestTest();
        Map<String, Object> map = new HashMap<>();
        map.put("name", "    123456    ");
        map.put("age", "    123");
        map.put("address", "    北京    ");
        List<String> list = new ArrayList<String>() {{
            this.add("name");
            this.add("age");
        }};
        Student student = TrimStringUtil.stringTrimDate(map, new TypeReference<Student>() {}, true, list);
        System.out.println(JSONObject.toJSONString(student));
}

结果

代码语言:json
复制
{"address":"    北京    ","age":"123","name":"123456"}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 坐标
  • 完整代码
  • 测试类
  • 结果
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档