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
    
    

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

  • 相关阅读:
    量身打造自己的MyEclipse(多图)
    DevExpress v17.2新版亮点—WPF篇(五)
    DevExpress WPF入门指南:绑定编辑器对话框
    MyEclipse 2017 Stable 2.0发布|附下载
    springmvc常用注解标签详解
    什么是SpringMVC?
    SpringBoot页面渲染
    怎样理解Spring的IOC和AOP?
    LESS 原理,一款css的预处理程序Less的使用
    移动端web app要使用rem实现自适应布局:font-size的响应式
  • 原文地址:https://www.cnblogs.com/planted/p/15182610.html
Copyright © 2011-2022 走看看