在开发使用spilit切割字符串时 发现一个有趣的现象 若切割符在开头 则会多出一个空字符串
如果不需要那个字符串可使用StringUtils中的spilit进行切割,可去除空字符串
package com.zj.test; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; public class Test2 { public static void main(String[] args){ String test1 = "1|2|"; String[] testA = test1.split("\\|"); System.out.println("length===>"+testA.length+" testA===>"+ArrayUtils.toString(testA)); String test2 = "|1|2"; String[] testB = test2.split("\\|"); System.out.println("length===>"+testB.length+" testB===>"+ArrayUtils.toString(testB)); String[] testC = StringUtils.split(test2 ,"\\|"); System.out.println("length===>"+testC.length+" testC===>"+ArrayUtils.toString(testC)); } }
请求结果如下: