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
    
    

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

  • 相关阅读:
    程序员高效学习
    红黑树(平衡操作详解)
    【设计模式】JDK源码中用到的设计模式
    pymysql.err.InternalError: (1205, 'Lock wait timeout exceeded; try restarting transaction')错误处理
    UPC:ABS
    洛谷:P1182:数列分段`Section II`
    python:数据库连接操作入门
    2018百度之星资格赛:1002:子串查询
    Educational Codeforces Round 48 (Rated for Div. 2)——A. Death Note ##
    python:pip命令使用
  • 原文地址:https://www.cnblogs.com/planted/p/15182610.html
Copyright © 2011-2022 走看看