2016/09/21 java split usage, 2016 split

Source: Internet
Author: User

2016/09/21 java split usage, 2016 split

Public String [] split (String regex) default limit is 0

Public String [] split (String regex, int limit)

When limit is greater than 0, n-1 times are applied.

public static void main(String[] args) {        String s = "boo:and:foo";        String[] str = s.split(":",2);        System.out.print(str[0] + "," + str[1]);  }

Result:

boo,and:foo

When limit is less than 0, the application is unlimited.

public static void main(String[] args) {        String s = "boo:and:foo";        String[] str = s.split(":",-2);        for(int i = 0 ; i < str.length ; i++){            System.out.print(str[i] + " ");        }  }

Result:

boo and foo

When limit = 0, apply unlimited times and omit the null string at the end

public static void main(String[] args) {        String s = "boo:and:foo";        String[] str = s.split("o",-2);        for(int i = 0 ; i < str.length ; i++){            if( i < str.length - 1)            System.out.print("(" + str[i] + "),");            else            System.out.print("(" + str[i] + ")");        }  }

Result:

(b),(),(:and:f),(),()
public static void main(String[] args) {        String s = "boo:and:foo";        String[] str = s.split("o",0);        for(int i = 0 ; i < str.length ; i++){            if( i < str.length - 1)            System.out.print("(" + str[i] + "),");            else            System.out.print("(" + str[i] + ")");        }  }

Result:

(b),(),(:and:f)

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.