Logo Search packages:      
Sourcecode: libcommons-lang-java version File versions  Download package

static String [] org::apache::commons::lang::StringUtils::splitByWholeSeparator ( String  str,
String  separator,
int  max 
) [inline, static]

Splits the provided text into an array, separator string specified. Returns a maximum of max substrings.

The separator(s) will not be included in the returned String array. Adjacent separators are treated as one separator.

A null input String returns null. A null separator splits on whitespace.

 StringUtils.splitByWholeSeparator(null, *, *)               = null
 StringUtils.splitByWholeSeparator("", *, *)                 = []
 StringUtils.splitByWholeSeparator("ab de fg", null, 0)      = ["ab", "de", "fg"]
 StringUtils.splitByWholeSeparator("ab   de fg", null, 0)    = ["ab", "de", "fg"]
 StringUtils.splitByWholeSeparator("ab:cd:ef", ":", 2)       = ["ab", "cd:ef"]
 StringUtils.splitByWholeSeparator("ab-!-cd-!-ef", "-!-", 5) = ["ab", "cd", "ef"]
 StringUtils.splitByWholeSeparator("ab-!-cd-!-ef", "-!-", 2) = ["ab", "cd-!-ef"]
 

Parameters:
str the String to parse, may be null
separator String containing the String to be used as a delimiter, null splits on whitespace
max the maximum number of elements to include in the returned array. A zero or negative value implies no limit.
Returns:
an array of parsed Strings, null if null String was input

Definition at line 2184 of file StringUtils.java.

References equals(), and split().

                                                                                          {
        if (str == null) {
            return null;
        }

        int len = str.length() ;

        if (len == 0) {
            return ArrayUtils.EMPTY_STRING_ARRAY;
        }

        if ( ( separator == null ) || ( "".equals( separator ) ) ) {
            // Split on whitespace.
            return split( str, null, max ) ;
        }


        int separatorLength = separator.length() ;

        ArrayList substrings = new ArrayList() ;
        int numberOfSubstrings = 0 ;
        int beg = 0 ;
        int end = 0 ;
        while ( end < len ) {
            end = str.indexOf( separator, beg ) ;

            if ( end > -1 ) {
                if ( end > beg ) {
                    numberOfSubstrings += 1 ;

                    if ( numberOfSubstrings == max ) {
                        end = len ;
                        substrings.add( str.substring( beg ) ) ;
                    } else {
                        // The following is OK, because String.substring( beg, end ) excludes
                        // the character at the position 'end'.
                        substrings.add( str.substring( beg, end ) ) ;

                        // Set the starting point for the next search.
                        // The following is equivalent to beg = end + (separatorLength - 1) + 1,
                        // which is the right calculation:
                        beg = end + separatorLength ;
                    }
                } else {
                    // We found a consecutive occurrence of the separator, so skip it.
                    beg = end + separatorLength ;
                }
            } else {
                // String.substring( beg ) goes from 'beg' to the end of the String.
                substrings.add( str.substring( beg ) ) ;
                end = len ;
            }
        }

        return (String[]) substrings.toArray( new String[substrings.size()] ) ;
    }


Generated by  Doxygen 1.6.0   Back to index