String splitting functions in Java

Source: Internet
Author: User

The Split function in Java is not the same as the Split function in JS.

In Java, we can use split to split a string into a specified delimiter, and then return an array of strings, followed by an example of string.split usage and considerations:

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, and 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 this option is omitted, a single element array containing the entire string is returned.

    • Limit

Options are available. This value is used to limit the number of elements in the returned array (that is, most of them are split into several array elements, only if they are positive)

The result of the split method is an array of strings, which are decomposed in the stingobj where each occurrence of the separator occurs. Separator is not returned as part of any array element.
Example 1:

1       String str= "Java string split test"2       string[] Strarray=str.split (""3        for (int i = 0; i < strarray.length; i++4           

Will output:

Java
String
Split
Test

Example 2:

1       String str= "Java string split test"2       string[] Strarray=str.split ("", 2);  3 for (int i = 0; i < strarray.length; i++            4           

Will output:

Java
String Split test

Example 3:

1       String str= "192.168.0.1"2       string[] Strarray=str.split ("."  3        for (int i = 0; i < strarray.length; i++4           

The result is nothing output, will split (".") Instead of split ("\ \"), the correct result will be output:

192
168
0
1

Experience Sharing:
1, the delimiter is "." (No output), "|" (Cannot get the correct result) when the escape character, "*", "+" when an error throws an exception, must be preceded by adding "\ \", such as split (\\|);
2, if "\" as a separate, you have to write this: String.Split ("\\\\"), because in Java is "\ \" to denote "\", the string must be written like this: string str= "a\\b\\c";
The escape character must be added "\ \";
3. If you have more than one delimiter in a string, you can use the ' | ' As a hyphen, for example: string str= "Java string-split#test", you can use Str.split ("|-|#") to separate each string;

String splitting functions in Java

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.