Split method ----> JAVA

Source: Internet
Author: User

The Java. lang package has a string class method: Split ("separator" [, limit]). This method returns an array.

Separator indicates the delimiter and limit indicates the number of elements in the returned array.

Note:
If "." and "|" escape characters exist in the separator, \: Must be added before them \\:
If ". ", must be written as follows: string. split ("\\. "), in this way, the correct separation is not allowed using string. split (". ");
If "|" is used as the separator, it must be written as follows: string. split ("\ |"), in order to correctly separate, cannot use string. split ("| ");
If a string contains multiple separators, you can use "|" as a hyphen, for example, "A = 1 and B = 2 or C = 3 ", you can use string to separate all three. split ("and | or ");
Limit groups the entire string. The grouping element contains all the characters in the original string! Example 2:

Example 1:

Public class splitdemo {
Public static string [] Ss = new string [20];
Public splitdemo (){
String S = "The rain in Spain falls mainly in the plain .";
// Separate each space character.
Ss = S. Split ("");
}
Public static void main (string [] ARGs ){
Splitdemo demo = new splitdemo ();
For (INT I = 0; I <ss. length; I ++)
System. Out. println (ss [I]);
}
}
Program result:
The
Rain
In
Spain
Falls
Mainly
In
The
Plain

Example 2:

Public class splitdemo {
Public static string [] Ss = new string [20];
Public splitdemo (){
String S = "The rain in Spain falls mainly in the plain .";
Ss = S. Split ("", 2); // separate each space character.
}
Public static void main (string [] ARGs ){
Splitdemo demo = new splitdemo ();
For (INT I = 0; I <ss. length; I ++)
System. Out. println (ss [I]);
}
}

Program result:

The
Rain in Spain falls mainly in the plain

Example 3:

Public class splitdemo3 {
Public static string [] Ss = new string [20];
Public splitdemo3 (){
String S = "The rain in Spain falls mainly in the plain .";
Ss = S. Split ("", 20); // separate each space character
}
Public static void main (string [] ARGs ){
Splitdemo3 demo = new splitdemo3 ();
For (INT I = 0; I <ss. length; I ++)
System. Out. println (ss [I]);
}
}
Program result:

The
Rain
In
Spain
Falls
Mainly
In
The
Plain.

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.