Java split usage Summary

Source: Internet
Author: User

Public String [] Split (string RegEx, int limit) Splits a string based on a regular expression.
The array returned by this method contains each substring of this string, which is terminated by another substring that matches the given expression or ends by the string. The substrings in the array are arranged in the order of the strings. If the expression does not match any part of the input, the result array has only one element, that is, this string.

The limit parameter controls the number of times that the mode applies, thus affecting the length of the result array. If the limit N is greater than 0, the mode will be applied N-1 times at most, and the length of the array will not be greater than N, in addition, the last entry of the array will contain all input that exceeds the last matching delimiter. If n is not positive, the pattern will be applied as many times as possible, and the array can be of any length. If n is zero, the mode will be applied as many times as possible, the array can have any length, and the trailing null string will be discarded.

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 );
}

}

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 .";
// Separate each space character.
Ss = S. Split ("", 2 );
}

Public static void main (string [] ARGs ){
Splitdemo demo = new splitdemo ();
For (INT I = 0; I <ss. length; I ++)
System. Out. println (SS );
}

}

Program result:
The
Rain in Spain falls mainly in the plain.


Example 3:
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 ("", 20 );
}

Public static void main (string [] ARGs ){
Splitdemo demo = new splitdemo ();
For (INT I = 0; I <ss. length; I ++)
System. Out. println (SS );
}

}

Program result:
The
Rain
In
Spain
Falls
Mainly
In
The
Plain.


Example 4:
Public class splitdemo {

Public static void main (string [] ARGs ){

String value = "192.168.128.33 ";
String [] names = value. Split (".");
For (INT I = 0; I <names. length; I ++ ){
System. Out. println (names );
}

}
}

Running result:

Yes. No error! No output!

Http://blog.csdn.net/lizhengnanhua/article/details/7994159

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.