Java--string.split () function

Source: Internet
Author: User

String[in Java Doc] Java.lang.String.split (String regex)

Splits this string around matches of the given regular expression.

This method works as if is invoking the Two-argument split method with the given expression and a limit argument of zero. Trailing empty strings is therefore not included in the resulting array.

The string "Boo:and:foo", for example, yields the following results with these expressions:

Regex Result
: {"Boo", "and", "foo"}
O {"B", "", ": And:f"}
Parameters:
regex The delimiting regular expression
Returns:
The
array of strings computed by splitting this string around matches of the given regular expression
Throws:
patternsyntaxexception-if The regular expression ' s syntax is invalid
Since:
1.4
See Also:
Java.util.regex.Pattern
@spec
JSR-51
but pay attention to the following points:

(1) If you use "." As a separate word must be the following string.split ("\ \"), so as to

The right separation can not be used String.Split (".");

For example, this code:

String str= "123.456"; String []arrstr=str.split ("."); System.out.println (arrstr.length);

The result is:

and

String str= "123.456"; String []arrstr=str.split ("\ \"); System.out.println (arrstr.length);

The result is:

 

Reason is

Splits this string around matches of the given regularexpression. A regular expression is required so the form of the regular expression must be satisfied,

Therefore, escaping is required.

The source code for the split () function is given below for reference:

  PublicString[] Split (String regex,intlimit) {        /*FastPath If the Regex is a (1) One-char String and this character are not one of the regex ' s Meta CH Aracters ". $| () [{^?*+\\], or (2) Two-char String and the first char is the backslash and the second are not the ASCII         Digit or ASCII letter. */        CharCH = 0; if((Regex.value.length = = 1 && ". $| () [{^?*+\\]. indexOf (ch = regex.charat (0)) = = =-1) | |(Regex.length ()= = 2 &&Regex.charat (0) = = ' \ \ ' &&((Ch= Regex.charat (1))-' 0 ') | (' 9 '-ch)) < 0 &&(Ch-' A ') | (' Z '-ch)) < 0 &&(Ch-' A ') | (' Z '-ch)) < 0)) &&(Ch< Character.min_high_surrogate | |CH>character.max_low_surrogate)) {            intOff = 0; intNext = 0; BooleanLimited = limit > 0; ArrayList<String> list =NewArraylist<>();  while(Next = indexOf (ch, off))! =-1) {                if(!limited | | list.size () < limit-1) {list.add (substring (off, next)); Off= next + 1; } Else{// last one//assert (list.size () = = limit-1);list.add (substring (off, value.length)); Off=value.length;  Break; }            }            //If no match is found, return this            if(Off = = 0)                return Newstring[]{ This}; //ADD remaining segment            if(!limited | | list.size () <limit) list.add (substring (off, value.length)); //Construct Result            intResultSize =list.size (); if(Limit = = 0)                 while(resultsize > 0 && list.get (resultSize-1). Length () = = 0) resultsize--; String[] Result=NewString[resultsize]; returnList.sublist (0, resultsize). ToArray (result); }        returnPattern.compile (Regex). Split ( This, limit); }

Public string[] Split (String regex) {
Return split (regex, 0);
}




Java--string.split () function

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.