zoukankan      html  css  js  c++  java
  • split(",")与split(",",-1)的区别

    split(",")与split(",",-1)的区别

    下面通过两种情况说明二者的区别

    第一种:字符串最后一位是要切割符

    代码:

    package com.yyy.test;
    
    public class testSplit {
        public static void main(String[] args) {
            String aaa="a,b,c,d,,,,,,";
            String[] split = aaa.split(",");
            System.out.println(split.length);
            for(String item:split){
                System.out.println(item+"==========");
            }
            System.out.println("1111111111111111111111111111111111111111111111111");
            String[] split1 = aaa.split(",", -1);
            System.out.println(split1.length);
            for (String item:split1){
                System.out.println(item+"==========");
            }
        }
    }
    
    

    执行结果

    4
    a==========
    b==========
    c==========
    d==========
    1111111111111111111111111111111111111111111111111
    10
    a==========
    b==========
    c==========
    d==========
    ==========
    ==========
    ==========
    ==========
    ==========
    ==========
    
    Process finished with exit code 0
    
    

    总结:
    第一种情况,二者的区别为,如果最后n位都为切割符则split(",")不会继续切割,而split(",",-1)会继续切割



    第二种情况 字符串最后一位不为切割符

    代码

    package com.yyy.test;
    
    public class testSplit2 {
        public static void main(String[] args) {
            String aaa="a,b,c,d,,,,,,e";
            String[] split = aaa.split(",");
            System.out.println(split.length);
            for(String item:split){
                System.out.println(item+"==========");
            }
            System.out.println("1111111111111111111111111111111111111111111111111");
            String[] split1 = aaa.split(",", -1);
            System.out.println(split1.length);
            for (String item:split1){
                System.out.println(item+"==========");
            }
        }
    }
    

    执行结果

    
    4
    a==========
    b==========
    c==========
    d==========
    1111111111111111111111111111111111111111111111111
    10
    a==========
    b==========
    c==========
    d==========
    ==========
    ==========
    ==========
    ==========
    ==========
    ==========
    
    Process finished with exit code 0
    
    

    总结
    第二种情况二者没有区别

  • 相关阅读:
    每位设计师都应该拥有的50个CSS代码片段-1
    JS OO库的选择
    Dean Edwards -- Base.js(译)
    Jquery中find、filter等几种用法的差异
    深入Java虚拟机读书笔记第三章安全
    深入Java虚拟机读书笔记第二章平台无关性
    深入Java虚拟机读书笔记第一章Java体系结构介绍
    ThinkInJava4读书笔记之第七章隐藏实施过程
    Java反射机制(转载)
    linux常用svn命令(转载)
  • 原文地址:https://www.cnblogs.com/planted/p/15182610.html
Copyright © 2011-2022 走看看