. NET to java.06. The difference between the split of a string

Source: Internet
Author: User

In Java, a feature that separates a string like "1|2|3|4" into an array

This problem can be stumped by more than 10 years of development experience. NET code farmer?

        // Java code        String s= "1|2|3";        string[] Array=s.split ("|");   

As a result, the array that comes out is this ghost look 1,|,2,|,3

Take a closer look at the name of the parameter that split passed in, a regex, a regular expression, an epiphany, to escape the regular expression ...

        // Java code        String s= "1|2|3";        string[] Array=s.split ("\\|");

After the problem is solved, continue to hand the cheap research this split, looking for. NET inside my favorite option where is Stringsplitoptions.removeemptyentries?

Do an experiment.

        // Java code        String s= ",,,";        string[] Array=s.split (",");         // The result of array is

Does the default with Removeemptyentries feature?

Then try again ...

        // Java code        String s= ",,, 3,,,";        string[] Array=s.split (",");         // "1", "2", "" "," "," 3 "

What the hell is the result??? Removeemptyentries of the tail method, automatically discard the trailing empty elements ? Old yards with. NET can't find the channel ah ...

Well, even so, the empty elements in the middle I can filter, if I do not want to abandon the empty tail of the elements can be the whole?

Baidu has a half-day, found a solution, if you want to keep the tail empty elements, to pass Limit=-1

        // Java code        String s= ",,, 3,,,";        string[] Array//  Incoming-1, keep trailing empty elements         //  "1", "2", "" "", "3", "" "," ""

Okay, I'll go on to the second parameter, limit.

 Public int limit)

The literal meaning should be to limit the number of split returned array elements,

The meaning of this limit is the same as. NET is very similar to the Count parameter

// C # code  Public int count, stringsplitoptions options)

If limit or count=n, indicates that the number of matches is n-1

 //  C # code  String s = Span style= "color: #800000;" > " 1|2|3| | |            4   ; string[] Array  = S.split (new  string[] { " /span>|  }, 2   //  The result of the array is {"1", "2|3| | | 4 "}  
        // Java code        String s= ",,, 3,,,";        string[] Array=s.split (",", 2);          // result of Array {"1", "2,,, 3,,,"}

See here, I think I understand.

And I'm depressed by the tests below.

        // Java code        String s1= "";        String[] array1=s1.split (",");         // The result of the array1 is {""} array with 1 empty elements         String s2= ",";        String[] array2=s2.split (",");         // The result of the array2 is that the {} array contains 0 elements

It turns out I really want 12315 complaints ...

. NET to java.06. The difference between the split of a string

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.