zoukankan      html  css  js  c++  java
  • Spring中常用的工具类StringUtils、DateUtils、CollectionUtils等

    一、StringUtils

    1、Spring提供的StringUtils

     /**
         * Spring自带的StringUtils
         */
        @Test
        void testStringUtils01(){
            boolean b = StringUtils.hasText("  "); //可用
            boolean empty = StringUtils.isEmpty("  "); //可用
            System.out.println(b); //false  认为没有文本  我们想要的
            System.out.println(empty); //false  认为不为空   空格不是null 也是我们想要的
    
            //测试结果  不是想要的结果
            String[] split = StringUtils.split("ni shi ge cao bao", " ");
            assert split != null;
            for (String s:split) {
                System.out.println(s);
            }
    
            //测试结果 单词之间的空格是一个没有问题,多个空格就报错
            String[] strings = StringUtils.tokenizeToStringArray("ni shi cao bao", " ");
            for (String s : strings) {
                System.out.println(s);
            }
        }

    2、Commons-lang3 提供的StringUtils工具类

       /**
         * commons-lang3 提供的StringUtils工具类
         */
        @Test
        void testStringUtils02() {
            boolean notBlank = StringUtils.isNotBlank("  ");
            boolean blank = StringUtils.isBlank("  ");
            System.out.println(notBlank);//false
            System.out.println(blank);//true
            boolean empty = StringUtils.isEmpty(" ");
            System.out.println(empty);//false
            boolean contains1 = StringUtils.contains("你是哪里人", "你");
            boolean contains2 = StringUtils.contains("你是哪里人", "哪里人");
            System.out.println(contains1);//true
            System.out.println(contains2);//true
    
            String replace1 = StringUtils.replace("你是哪里人 ,你是哪里人", "哪里人", "我的心上人");
            System.out.println(replace1);//你是我的心上人 ,你是我的心上人
    
            String replace2 = StringUtils.replaceOnce("你是哪里人 ,你是哪里人", "你是哪里人", "心上人");
            System.out.println(replace2);//心上人 ,你是哪里人
    
            int compare1 = StringUtils.compare("you are in my heart", "you are in my heart");
            int compare2 = StringUtils.compareIgnoreCase("you are in my heart", "you are iN my heart");
    
            System.out.println(compare1); //0
            System.out.println(compare2); //0
    
            boolean b = StringUtils.startsWith("you are in my heart", "you");
            System.out.println(b);//true
        }

    二、DateUtils

    1、Commons-lang3 提供的DateUtils工具类

       /**
         * DateUtils
         */
        @Test
        void testDate(){
            Date date = new Date();
            Date date1 = DateUtils.addDays(date, 1);
            Date date2 = DateUtils.addHours(date1, 10);
            boolean sameDay = DateUtils.isSameDay(date1, date2);
            Date date3 = DateUtils.setYears(date1, 2030);
            System.out.println(sameDay);
            System.out.println(date3);
        }

    三、CollectionUtils(以下都是Spring框架提供的)

        /**
         * CollectionUtils
         */
        @Test
        void testCollections(){
            //判断HashMap是否为空
            HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
            boolean empty = CollectionUtils.isEmpty(objectObjectHashMap);
            System.out.println(empty);//true
    
            //判断ArrayList是否为空
            ArrayList<Object> objects = new ArrayList<>();
            boolean empty1 = CollectionUtils.isEmpty(objects);
            System.out.println(empty1);//true
    
            //数组转集合
            String[] a ={"wo","he","ni"};
            List list = CollectionUtils.arrayToList(a);
            Object o2 = CollectionUtils.lastElement(list);
            System.out.println(list);//[wo, he, ni]
            System.out.println(o2);//ni
        }

    四、AlternativeJdkIdGenerator(UUID工具类生成)

       /**
         * UUID工具类生成
         */
        @Test
        void testAlternativeJdkIdGenerator(){
            AlternativeJdkIdGenerator a=new AlternativeJdkIdGenerator();
            UUID uuid = a.generateId();
            String id = StringUtils.replace(uuid.toString(), "-", "");
            System.out.println(id);//生成去掉“-”的32位uuid  例如:b079c3d726b5b7ae6d432d2e2a0831b3
        }

    五、PathMatcher(路径匹配器)

        /**
         * PathMatcher
         */
        @Test
        void testPathMatcher(){
            PathMatcher pathMatcher = new AntPathMatcher();
            System.out.println();
    
            // 精确匹配
            System.out.println(pathMatcher.match("/test", "/test"));//true
            System.out.println(pathMatcher.match("test", "/test"));//false
    
            //测试占位符?
            System.out.println(pathMatcher.match("t?st", "test"));//true
            System.out.println(pathMatcher.match("te??", "test"));//true
            System.out.println(pathMatcher.match("tes?", "tes"));//false
            System.out.println(pathMatcher.match("tes?", "testt"));//false
    
            //测试通配符*
            System.out.println(pathMatcher.match("*", "test"));//true
            System.out.println(pathMatcher.match("test*", "test"));//true
            System.out.println(pathMatcher.match("test/*", "test/Test"));//true
            System.out.println(pathMatcher.match("*.*", "test."));//true
            System.out.println(pathMatcher.match("*.*", "test.test.test"));//true
            System.out.println(pathMatcher.match("test*", "test/")); //注意这里是false 因为路径不能用*匹配
            System.out.println(pathMatcher.match("test*", "test/t")); //false这同理
            System.out.println(pathMatcher.match("test*aaa", "testblaaab")); //这个是false 因为最后一个b无法匹配了 前面都是能匹配成功的
    
            //测试通配符** 匹配多级URL
            System.out.println(pathMatcher.match("/*/**", "/testing/testing"));//true
            System.out.println(pathMatcher.match("/**/*", "/testing/testing"));//true
            System.out.println(pathMatcher.match("/bla/**/bla", "/bla/testing/testing/bla/bla")); //这里也是true哦
            System.out.println(pathMatcher.match("/bla*bla/test", "/blaXXXbl/test"));//false
    
            System.out.println(pathMatcher.match("/????", "/bala/bla"));//false
            System.out.println(pathMatcher.match("/**/*bla", "/bla/bla/bla/bbb"));//false
    
            System.out.println(pathMatcher.match("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing/"));//true
            System.out.println(pathMatcher.match("/*bla*/**/bla/*", "/XXXblaXXXX/testing/testing/bla/testing"));//true
            System.out.println(pathMatcher.match("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing"));//true
            System.out.println(pathMatcher.match("/*bla*/**/bla/**", "/XXXblaXXXX/testing/testing/bla/testing/testing.jpg"));//true
            System.out.println(pathMatcher.match("/foo/bar/**", "/foo/bar"));//true
    
            //这个需要特别注意:{}里面的相当于Spring MVC里接受一个参数一样,所以任何东西都会匹配的
            System.out.println(pathMatcher.match("/{bla}.*", "/testing.html"));//true
            System.out.println(pathMatcher.match("/{bla}.htm", "/testing.html")); //这样就是false了
        }
  • 相关阅读:
    就为了一个原子操作,其他CPU核心罢工了
    浅谈JVM和垃圾回收
    简单了解一下K8S,并搭建自己的集群
    WebAssembly完全入门——了解wasm的前世今身
    【简单了解系列】从基础的使用来深挖HashMap
    【俗话说】换个角度理解TCP的三次握手和四次挥手
    两分钟让你明白Go中如何继承
    游戏服务器和Web服务器的区别
    Go中使用seed得到相同随机数的问题
    从web到游戏,走出舒适区
  • 原文地址:https://www.cnblogs.com/donleo123/p/14140627.html
Copyright © 2011-2022 走看看