Java. Lang. String split () method

Source: Internet
Author: User

Java. Lang. String split () method

 

The test is as follows:
String A = "Hello ,,,";
String [] Reverse r = A. Split (",");
System. Out. println ("comment R. Length =" + comment R. Length );

Output result: Required R. Length = 1. This array has only one element: [Hello]
 
--------------------

String A = "Hello ,,,";
String [] Reverse r = A. Split (",", 0 );
System. Out. println ("comment R. Length =" + comment R. Length );
Output result: Required R. Length = 1. This array has only one element: [Hello]

--------------------

String A = "Hello ,,,";
String [] Reverse r = A. Split (",", 2 );
System. Out. println ("comment R. Length =" + comment R. Length );
Output result: Required R. Length = 2. This array has two elements: [Hello] [,]

--------------------
 
String A = "Hello ,,,";
String [] Reverse r = A. Split (",",-1); // The parameter is-1. This is often the result we want.
System. Out. println ("comment R. Length =" + comment R. Length );
Output result: Required R. Length = 4. The array has four elements: [Hello] [] [] [].

 

The java5.0 API manual says:

--------------------

Public String [] Split (string RegEx,
Int limit)

Splits the string based on the given 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, use these parameters to generate the following results for the string "Boo: And: foo":

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, which is split by matching the given regular expression to generate this array.

Throw:
Patternsyntaxexception-if the regular expression syntax is invalid

Start with the following versions:
1.4

--------------------

 

In addition, in regular expressions, ".", "|", "+", and "*" are all escape characters. In Split (), you must add "//".

For example, split ("//."), split ("// |"), split ("// +"), and split ("//*").

[End]

2008/11/11 in Beijing

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.