Java -- String. split () function, javastring. split

Source: Internet
Author: User

Java -- String. split () function, javastring. split
In java doc, there are String [] java. lang. String. split (String regex)

Splits this string around matches of the given regular expression.

This method works as if by invoking the two-argumentsplitMethod with the given expression and a limit argument of zero. Trailing empty strings are therefore not supported ded 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:
RegexThe 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
Note the following:

(1) If "." is used as the separator, String. split ("\.") must be written as follows.

String. split (".");

For example, this Code:

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

Result:

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

Result:

 

The reason is:

Splits this string around matches of the given regular expression. It requires a regular expression, so it must meet the regular expression form,

Therefore, escape is required.

The source code of the split () function is provided below for your reference:

 public String[] split(String regex, int limit) {        /* fastpath if the regex is a         (1)one-char String and this character is not one of the            RegEx's meta characters ".$|()[{^?*+\\", or         (2)two-char String and the first char is the backslash and            the second is not the ascii digit or ascii letter.         */        char ch = 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))        {            int off = 0;            int next = 0;            boolean limited = limit > 0;            ArrayList<String> list = new ArrayList<>();            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 was found, return this            if (off == 0)                return new String[]{this};            // Add remaining segment            if (!limited || list.size() < limit)                list.add(substring(off, value.length));            // Construct result            int resultSize = list.size();            if (limit == 0)                while (resultSize > 0 && list.get(resultSize - 1).length() == 0)                    resultSize--;            String[] result = new String[resultSize];            return list.subList(0, resultSize).toArray(result);        }        return Pattern.compile(regex).split(this, limit);    }

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




 


What is the use of the split function of the String class in java? Are the first and last elements of the array output after I split the string empty?

It is useless. Just a few lines of code that you can run on your own.
String aa = "a, B, c, d, e, f ";
String [] bb = aa. split (",");
For (int I = 0; I <bb. length; I ++ ){
System. out. println (bb [I]);
}

Java code: Use of split

The parameter of the split function is not a string but a regular expression.
+ The number itself is a special character in a regular expression and needs to be escaped.
String [] array = str. split ("\\+ ");

Related Article

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.