Split

Java-Split

Split

The split() method of String is used to split the string around matches of the given regular expression. It has the following form:

Split

The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. The substrings in the array are in the order in which they occur in this string. If the expression does not match any part of the input then the resulting array has just one element, namely this string. The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit is positive then the pattern will be applied at most limit – 1 times, the array’s length will be no greater than limit, and the array’s last entry will contain all input beyond the last matched delimiter. For example:

Split

When there is a positive-width match at the beginning and the end of this string then an empty leading substring is included at the beginning and the end of the resulting array. For example:

Split

If the limit is zero then the pattern will be applied as many times as possible, the array can have any length. When there is a zero-width match at the beginning of this string then an empty leading substring is included at the beginning of the resulting array and trailing empty strings will be discarded. For example:

Split

If the limit is negative then the pattern will be applied as many times as possible and the array can have any length. For example:

Split

Another Form of Split Method

Split

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. For example:

Split

Trailing empty strings are not included in the resulting array. Because of the limit argument is considered as zero. For example:

Split


Program

Split

Program Source

public class Javaapp {

    public static void main(String[] args) {

        String str = ",ABCD,EFGH,IJKL,,MNOP,QRST,";

        String strarray[] = str.split(",", 10);

        for (int i = 0; i < strarray.length; i++) {             System.out.println("strarray[" + i + "]-->" + strarray[i]);
        }
    }
}

Leave a Comment