JAVA one or more spaces split string

Source: Internet
Author: User

Knowledge Supplement
    1. The split method of string supports regular expressions;

    2. The regular expression \s represents any white space character that matches + one or more occurrences.

With the above supplementary knowledge, the following content is very well understood.

A. String to be split

The string to be split is as follows:

str = "a  b     c  d e f      g"

Where the whitespace in the string is: a single space, multiple Spaces, tab tabs.

Second, use one or more spaces to split the string

The correct code is as follows:

String [] arr = str.split("\\s+");for(String ss : arr){ System.out.println(ss);}

As a comparison, the error code is as follows:

String [] arr2 = str.split(" ");for(String ss : arr2){ System.out.println(ss);}
Third, the result of segmentation

To split the result with the correct code:

abcdefg

To split the result with an error code:

abcdefg
四:编写的程序

     Public Static void Main (String args[]) {        = "good12 morning34 good56 night78";         // The output        of the array after the split of string String[] Tt=line.split ("\\s+");          for (String s:tt)        {            System.out.println (s);        }        System.out.println ("#####");}

The output result is
Good12
Morning34
Good56
night78

 

JAVA one or more spaces split string

Related Article

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.