commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Al Chou <>
Subject [lang] unexpected StringUtils.split behavior (was RE: suggestion for new StringUtils.method)
Date Thu, 20 Nov 2003 06:21:33 GMT
I guess my previous post got lost in the noise, so I'm reposting.  I have two
new StringUtils.split methods that can split a string at occurrences of a
substring rather than splitting at the individual characters in the specified
delimiter string.

While testing, I discovered that my expectations for the behavior of the split(
*, ..., int max ) methods didn't match their actual behavior.  I expected to
get a maximum of "max" substrings, all of which were delimited in the parent
string by the specified delimiters.  Instead, what you get is "max - 1" such
substrings, plus the rest of the parent string as the final result substring. 
This behavior seems counter to what StringTokenizer would do, which is
surprising, given the Javadoc comments about using the split methods as
alternatives to StringTokenizer.

Currently, my tests reflect my expectations for the behavior, and I modified
the existing split( String, String, int ) method to match my expectations.  I
didn't want to submit such a change as a proposed patch without first getting
feedback from the community about whether my expectations are wrong.  I am
happy to submit only code that does not change the behavior of the existing
methods, if need be.


--- Al Chou <> wrote:
> This thread is a good entree for my question.  I was adding a new
> StringUtils.split method that can split a string using a whole string as the
> delimiter, rather than the characters within that string.  In running my
> JUnit tests, I discovered unexpected behavior in the existing method:
> String stringToSplitOnNulls = "ab   de fg" ;
> String[] splitOnNullExpectedResults = { "ab", "de" } ;
> String[] splitOnNullResults = StringUtils.split( stringToSplitOnNulls, null,
> 2
> ) ;
> assertEquals( splitOnNullExpectedResults.length, splitOnNullResults.length )
> ;
> for ( int i = 0 ; i < splitOnNullExpectedResults.length ; i+= 1 )
> {
>     assertEquals( splitOnNullExpectedResults[i], splitOnNullResults[i] ) ;
> }
> The result of the split call is
> "ab", "de fg"
> and it doesn't look to me like StringTokenizer's documentation implies this
> behavior....
> Al
> =====
> Albert Davidson Chou
>     Get answers to Mac questions at .

Albert Davidson Chou

    Get answers to Mac questions at .

Do you Yahoo!?
Free Pop-Up Blocker - Get it now

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message