Splits the provided text into an array, separator string specified. Returns a maximum of The separator(s) will not be included in the returned String array. Adjacent separators are treated as one separator.
A
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"]
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()] ) ;
}
|