Java Split Usage

Source: Internet
Author: User

Java.lang.string.split

Split method
Splits a string into substrings and returns the result as an array of strings.

Stringobj.split ([Separator,[limit]])

Stringobj
Required option. The String object or text to be decomposed. The object is not modified by the split method.

Separator
Options are available. A string or regular expression object that identifies whether one or more characters are used when separating the string. If you suddenly
This option returns a single-element array that contains the entire string.

Limit
Options are available. This value is used to limit the number of elements in the returned array.

Description
The result of the split method is an array of strings, which are decomposed in every position where separator occurs in stingobj.
。 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.";
Decompose at 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 results:
The
Rain
Inch
Spain
Falls
Mainly
Inch
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.";
Decompose at 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 results:
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.";
Decompose at 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 results:
The
Rain
Inch
Spain
Falls
Mainly
Inch
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]);
}

}
}

Operation Result:

Yes, it's not wrong! No output!
Let's take a look at the method signature of the split method:

Public string[] Split (string regex)
The name of the parameter here is the Regex, which is the regular expression (regex). This parameter is not a simple segmentation of the character, but a regular expression, look at the split method implementation code to strengthen our confidence:

Public string[] Split (string regex, int limit) {
return Pattern.compile (Regex). Split (this, limit);
}
Split's implementation directly calls the split method of the Matcher class. The reader already knows, ". "There are special meanings in regular expressions, so we must escape when we use them."
As long as the
string[] names = Value.split (".");
Switch
string[] names = value.split ("\ \");
You can do it.

Output Result:
192
168
128
33


Add a little bit more (this is a bit clearer in the Java Help documentation):

Public string[] Split (string regex,int limit) splits this string by matching a given regular expression.
The array returned by this method contains each substring of this string, terminated by another substring that matches the given expression, or terminated by the end of the string. The substrings in the array are arranged in the order in which they are in this string. If the expression does not match any part of the input, the resulting array has only one element, that is, the string.

The limit parameter controls the number of times the pattern is applied, thus affecting the length of the resulting array. If the limit n is greater than 0, the pattern will be applied up to n-1 times, the length of the array will not be greater than N, and the last item of the array will contain all inputs that exceed the last matching delimiter. If n is not positive, the pattern is applied as many times as possible, and the array can be any length. If n is zero, the pattern will be applied as many times as possible, the array can have any length, and the trailing empty string will be discarded.

For example, the string "Boo:and:foo" uses these parameters to produce the following results:

Regex Limit Results

: 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 form of method call Str.split (regex, N) produces exactly the same result as the following expression:

Pattern.compile (Regex). Split (str, n)

Parameters:
Regex-bound Regular expression
Limit-result threshold, as described above
Return:
A string array that splits this string according to the match of the given regular expression, resulting in this array
Thrown:
Patternsyntaxexception-If the syntax of the regular expression is invalid
Start from the following versions:
1.4


Public string[] Split (string regex) splits this string based on the match of the given regular expression.
The method acts as if the two-argument split method is called with the given expression and the limit parameter. Therefore, the trailing empty string is not included in the result array.

For example, the string "Boo:and:foo" yields results with these expressions:

Regex Results
: {"Boo", "and", "foo"}
o {"B", "", ": And:f"}

Parameters:
Regex-bound Regular expression
Return:
An array of strings that is generated by splitting the string according to the match of the given regular expression.
Thrown:
Patternsyntaxexception-If the syntax of the regular expression is invalid

Java Split Usage

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.