Share a quick split method for Java String split

Source: Internet
Author: User
Tags variable scope

The String.Split () method in Java is quite powerful, but the split () method uses regular expressions, which are relatively slow,

In fact, most scenarios do not need to use regular expressions, and below share a method that does not use regular expressions to separate strings.

The method guarantees the same output as the String.Split ().

Look directly at the code:

     Public Staticstring[] Split (string src,string delimeter) {string Srcstr=src; String Delimeterstr=Delimeter; if(srcstr==NULL){            return NULL; }        if(delimeterstr==NULL){            Throw NewIllegalArgumentException ("Delimeter should not is null"); }        if(Delimeterstr.equals ("") {//returns the string form of each character directly string[] array=Newstring[srcstr.length ()];  for(inti = 0;i<array.length;i++) {Array[i]=string.valueof (Srcstr.charat (i)); }            returnArray; }        if(Srcstr.length () >delimeterstr.length ()) {//source string length greater than delimiter string lengthinti =Srcstr.indexof (DELIMETERSTR); intj =i; intn = 0; intLastIndex = Srcstr.length ()-delimeterstr.length (); BooleanLaststringisdelimeter =false;  while(I >= 0) {n++; I= Srcstr.indexof (delimeterstr, i +delimeterstr.length ()); if(i = = LastIndex) {//Delimeter is the last string of the SRC, should not being countedLaststringisdelimeter =true;  Break; }} string[] array=NewString[n + 1]; N= i = 0;  while(J >= 0) {                if(J-i > 0) {Array[n++] =srcstr.substring (i, j); } Else if(J-i = = 0) {//Delimeter is neighbourarray[n++] = ""; } I= j +delimeterstr.length (); J=Srcstr.indexof (Delimeterstr, i); }            if(!laststringisdelimeter) {Array[n]=srcstr.substring (i); }            returnArray; } Else if(srcstr.length () = =delimeterstr.length ()) {//source string length equals delimiter string lengthif(Srcstr.equals (delimeterstr)) {return NewString[0]; } Else{string[] array=NewString[1]; array[0] =Srcstr; returnArray; }        } Else{//source string length less than delimiter string length, return source string string[] array directly=NewString[1]; array[0] =Srcstr; returnArray; }    }

Test code:

String src = "a.b.c.d.e.f.g.h.j.k.l."; SRC= "A.." B.. C.. D.. E.. F.. G.. H.. J.. K.. L; System.out.println ("First-->"); LongStart =System.nanotime (); String[] Array= Split (src, "..."); LongEnd =System.nanotime (); System.out.println ("Time:" + (End-start) + "ns"); System.out.println ("Size:" +array.length);  for(String s:array) {System.out.println (s); } System.out.println ("<--end"); System.out.println ("Second-->"); LongStart1 =System.nanotime (); String[] Array2= Src.split ("\\.\\.")); LongEnd1 =System.nanotime (); System.out.println ("Time:" + (END1-START1) + "ns"); System.out.println ("Size:" +array2.length);  for(String s:array2) {System.out.println (s); } System.out.println ("<--end");

Running results, it can be seen that, do not use regular expression, the speed is a bit faster, of course, this single test is not necessarily accurate, the reader can write their own multi-cycle averaging code to test.

It is important to note that the code is not locked for synchronization because the code does not have concurrency problems, the variable scope is always within the local variable table, and the JVM is thread independent.

I have a limited level, if there is a problem, please comment.

Reprint please indicate the source.

Share a quick split method for Java String split

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.