Problem where split function in Java becomes null after splitting

Source: Internet
Author: User

For some special cases, split does not retain the entire element after splitting

Here's an example:

String x = "a,,,,,,"x.split(",");  for (int i=0;i<y.length;i++) {    System.out.println (y[i]);}

Output Result:

The first letter is only split, and the empty string after the default is not preserved.

Later thought of using the StringUtils function, looked up, modified as follows:

String x = "a,,,,,,"stringutils.splitpreservealltokens(x, ",");  for (int i=0;i<y.length;i++) {    System.out.println (y[i]);}


Output Result:


public static string[] Splitpreservealltokens (String str, char Separatorchar)
Splits the string str into an array, with the character (Separatorchar) as the delimiter, and retains all the identities.

The StringUtils function is still very powerful, and the problem with strings is not going to look good here.
If I was in ACM, I knew this stuff, and a lot of problems were solved t_t

Problem where split function in Java becomes null after splitting

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.