Java split usage

Source: Internet
Author: User

Java. Lang. String. Split

Split Method
Splits a string into substrings and returns the result as a string array.

Stringobj. Split ([separator, [limit])

Stringobj
Required. String object or text to be decomposed. This object will not be modified by the split method.

Separator
Optional. A string or regular expression object that identifies whether a string is separated by one or more characters. If
Returns a single array of elements that contain the entire string.

Limit
Optional. This value is used to limit the number of elements in the returned array.

Note:
The result of the split method is a string array. In stingobj, the position where separator appears must be decomposed.
. The separator is not returned as part of any array element.

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 .";
// 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 [I]);
}

}

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 [I]);
}

}

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 [I]);
}

}
}

Running result:

Yes. No error! No output!
Let's take a look at the method signature of the split method:

Public String [] Split (string RegEx)
The parameter name here is RegEx, that is, regular expression (regular expression ). This parameter is not a simple delimiter, but a regular expression. After reading the implementation code of the split method, we are more confident:

Public String [] Split (string RegEx, int limit ){
Return Pattern. Compile (RegEx). Split (this, limit );
}
The split method of the matcher class directly called by the split implementation. Readers already know that "." has special meanings in regular expressions, so we must escape it when using it.
As long
String [] names = value. Split (".");
Change
String [] names = value. Split ("//.");
You can.

Output result:
192
168
128
33

Add a bit more (this is in the Java help document and is clearer ):

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.

For example, the string "Boo: And: foo" can use these parameters to generate the following results:

RegEx limit result

: 2 {"boo", "And: foo "}
: 5 {"boo", "and", "foo "}
:-2 {"boo", "and", "foo "}
O 5 {"B", "", ": And: F ","",""}
O-2 {"B", "", ": And: F ","",""}
O 0 {"B", "", ": And: F "}

This method calls Str. Split (RegEx, n) to produce the same result as the following expression:

Pattern. Compile (RegEx). Split (STR, n)

Parameters:
RegEx-bounded Regular Expression
Limit-result threshold, as described above
Return Value:
String Array. This string is split Based on the matching of the given regular expression to generate this array.
Throw:
Patternsyntaxexception-if the regular expression syntax is invalid
Start with the following versions:
1.4

Public String [] Split (string RegEx) splits the string based on the matching of the given regular expression.
This method is used to call the Two-Parameter split method by using the given expression and limit parameter 0. Therefore, the result array does not contain trailing null strings.

For example, the string "Boo: And: foo" produces results with the following expressions:

RegEx result
: {"Boo", "and", "foo "}
O {"B", "", ": And: F "}

Parameters:
RegEx-bounded Regular Expression
Return Value:
String Array, which is split by matching the given regular expression to generate this array.
Throw:

Patternsyntaxexception-if the regular expression syntax is invalid

Experience Sharing:
1. The separator is ". "(no output)," | "(cannot get the correct result) When escape characters," * "," + "error throws an exception, you must add "//" to the front, such as split (// | );
2. If you use "/" as the separator, you must write it as follows: string. split ("//"), because in Java, "//" is used to represent "/", the string must be written as follows: string STR = "A // B // C ";
Escape characters, which must contain "//";
3. If a string contains multiple separators, you can use "|" as a hyphen, for example, string STR = "Java string-split # test". You can use Str. split ("|-| #") separates each string;

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.