java split 坑

	一 , java split简单用法

  

public class SplitTest {
 public static void main(String[] args) {
  
  //一般分割
     String a="hello world ni hao";
     String[] array1=a.split(" ");
     System.out.println(array1[0]);
     System.out.println(array1.length);
  
 }

------------输出-------

hello
4

二,字符串末尾分隔符不能识别

该方法很常用,也很方便,但是当空格再字符串末尾的时候呢?下面看一下

 

 


public class SplitTest {
 public static void main(String[] args) {
  
  //一般分割
     String a="hello world ni hao ";
     String[] array1=a.split(" ");
     System.out.println(array1[0]);
     System.out.println(array1.length);
  
 }

}

 

---------输出----------

hello
4

为啥数组长度还是四个呢?这是它的误区之一,请注意此时的split()方法并不会识别末尾的字符,并分割,当要分割的字符串末尾数据为空时,应注意

避免使用此方法,后来找到了方法split(" ",-1),如下

 String a="hello world ni hao ";
     String[] array1=a.split(" ",-1);
     System.out.println(array1[0]);
     System.out.println(array1.length);

 

--------输出----------

hello
5

三 特殊符号的分割

public class SplitTest {
 public static void main(String[] args) {
  
  //特殊分割
  String a="hello|world|ni|hao";
  String[] array1=a.split("|");
     System.out.println(array1[0]);
     System.out.println(array1.length);
  
 }

-------输出------------


19

上面中竖线时特殊符号,应用右双斜杠转译后再分割 ,如下:

 

public class SplitTest {
 public static void main(String[] args) {
  
  //特殊分割
  String a="hello|world|ni|hao";
  String[] array1=a.split("\\|");
     System.out.println(array1[0]);
     System.out.println(array1.length);
  
 }

------输出----------------

hello
4

四 自定义split()方法,java中原生的split方法分割较长字符串时是比较低效的,需要自己重写split()方法,我自己写的分割方法如下(利用indexof)

,

  public static String[] newsplit(String strInfo, String strSplit) {
      //第1步计算数组大小
     int size  = 0;
     int index = 0;
     do{
      size++;
      index++;
      index = strInfo.indexOf(strSplit ,index);
     }while(index!=-1);
     String[] arrRtn= new String[size]; //返回数组
     //-------------------------------------------------------
     //第2步给数组赋值
     int startIndex = 0;
     int endIndex     = 0;    
     for(int i = 0; i < size; i++){
       endIndex = strInfo.indexOf(strSplit, startIndex);
        if(endIndex == -1) {
           arrRtn[i] = strInfo.substring(startIndex);
         } else {
          arrRtn[i] = strInfo.substring(startIndex, endIndex);
         }
      startIndex = endIndex+1;
     }
     return arrRtn;
      }

 

 public static void main(String[] args) {
  
  //特殊分割
  String a="hello|world|ni|hao|";
  //String[] array1=a.split("\\|");
  String[] array1=newsplit(a,"|");
     System.out.println(array1[0]);
     System.out.println(array1.length);
  
 }

-------输出------

hello
5

且此方法不需要转译特殊字符,因为原生split方法同样识别正则表达式,所以不是识别特殊字符

 

当你需要更高效的split方法时,StringUtils.split(str,"") 方法同样可用,比原生的split高效,该方法来自 apache-common的jar包,我用的是

commons-lang3-3.0.1.jar的包,但要注意StringUtils.split()对于字符串开头和结尾的分割符不识别,会默认省区,

 

 
————————————————

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值