Java string.split () usage

Source: Internet
Author: User

In Java, we can use split to divide a string into a specified delimiter, and then return an array of strings, followed by an example of string.split usage and considerations: 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 options. The String object or text to be decomposed, and the object is not modified by the split method.  separator  options 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 available. This value is used to limit the number of elements in the returned array (that is, to split up into several array elements, only for positive numbers) The result of the   split method is an array of strings, which is decomposed in stingobj where each occurrence of the separator occurs. Separator is not returned as part of any array element.     Example 1:       string str= "Java string split test";       string[] Strarray=str.split ("");       for (int i = 0; i < strarray.length; i++)            SYSTEM.OUT.PRINTLN (Strarray[i]);    outputs: java  string split test   Example 2:       string str= "Java string split test";        string[] Strarray=str.spliT ("", 2);//use limit to split up to 2 strings        for (int i = 0; i < strarray.length; i++)            SYSTEM.OUT.PRINTLN (Strarray[i]);  outputs:   java  String Split test     example 3:       string str= "192.168.0.1";     & nbsp String[] Strarray=str.split (".");        for (int i = 0; i < strarray.length; i++)            System.out . println (Strarray[i]);  result is nothing output, will split (".") Change to split ("\ \") to output the correct result: 192 168 0 1   experience sharing:  1, delimiter as "." (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 (\\|); &NBSP;&NBSP;&NBSP;2, if you use "\" 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";   Escape character, must be added "\ \",    3, if there is 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; 

Java string.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.