[java]_[The Pit in the split of the primary]_[string]

Source: Internet
Author: User


Scene:

1. Sometimes you need to use some characters as the value of the merge storage, such as @ as the delimiter, [email protected]@323232, with a property to store these 3 values, when used to take out split on it.

2. The problem is sometimes a value is missing, such as the first value is not the case, @[email protected] 2nd, 3 values are not the case [email protected]@, of course, also expected to return 3 values, but the back 2 values are null characters on the line.

In fact, the result is not so, even if 2, 3 values are not in the case [email protected]@ also returns a 1th value only, the array size is 1. This makes the person very puzzled, obviously has the delimiter, why when cannot see? So be careful when you program again.

Split is an inert search, and if there is no value in the back if there is no result, but if there is only a 3rd value, it is true that the array size is 3, and the first two are empty strings. That is, split follows a principle that returns the results and the array

The index must be the least number of groups that correspond.

3. Java's practice will make the handwriting C + + split people crazy, do C + + switch Java to pay attention to.

4. That is, the split of string cannot get a fixed number of splits even if the number of delimiters is specified.


Situation 1.

String value = "\n\n2"; string[] names = Value.split ("\ n"), for (int i = 0; i < names.length; i++) {System.out.println ("I:" +names[i]);}

Output:

I:i:i:2

Situation 2.

String value = "2\n\n"; string[] names = Value.split ("\ n"), for (int i = 0; i < names.length; i++) {System.out.println ("I:" +names[i]);}

Output:

I:2


Scenario 3:

String value = "\ n"; string[] names = Value.split ("\ n"), for (int i = 0; i < names.length; i++) {System.out.println ("I:" +names[i]);}

Output: No output, number is 0.

Reference:

http://tjuking.iteye.com/blog/1507855




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[java]_[The Pit in the split of the primary]_[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.