[ 
https://issues.apache.org/jira/browse/LANG-823?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18048551#comment-18048551
 ] 

Abhineet Kelley commented on LANG-823:
--------------------------------------

Hi [~ggregory] 

Sharing the same view as the comment by [~chonton], for curiosity may I please 
know why have we kept a different behaviour for {{*StringUtils.split*}} from 
the native {{*String.split*}} method in case of empty strings?
{noformat}
StringUtils.split("", *)  ->  []

"".split(*)               ->  [""]{noformat}
 

Actually, there was a recent instance wherein to reduce the cognitive 
complexity of our production code, I had used *{{StringUtils.split}}* to handle 
null strings internally. However when our unit tests failed I observed that it 
behaves differently from native Java in case of empty strings. Therefore I had 
to switch to the native implementation to handle this case.

This was basically our previous code:
{noformat}
// -- continues --

String phoneNumbers = dto.getPhoneNumbers();
newDTO.setPhoneNumber("");

if (phoneNumbers != null) {
   newDTO.setPhoneNumber(phoneNumbers.split(",")[0]);
}

// -- continues --{noformat}
This is what I was hoping to do with *StringUtils* (commons-lang3 : 3.20.0) 
after the refactor:
{code:java}
String phoneNumbers = StringUtils.defaultIfBlank(dto.getPhoneNumbers(), 
StringUtils.EMPTY);
newDTO.setPhoneNumber(StringUtils.split(phoneNumbers, ",")[0]);{code}
But I eventually went with the one below because at this point, the variable 
{{*phoneNumbers*}} wouldn't be null anymore. Thus using StringUtils for 
splitting the string would be optional.
{code:java}
String phoneNumbers = StringUtils.defaultIfBlank(dto.getPhoneNumbers(), 
StringUtils.EMPTY);
newDTO.setPhoneNumber(phoneNumbers.split(",")[0]);{code}
This did however made me curious about the implementation we have in 
commons-lang3.

> StringUtils.split should handle empty strings the same as other content
> -----------------------------------------------------------------------
>
>                 Key: LANG-823
>                 URL: https://issues.apache.org/jira/browse/LANG-823
>             Project: Commons Lang
>          Issue Type: Improvement
>          Components: lang.*
>    Affects Versions: 2.5
>            Reporter: Mark Farnsworth
>            Assignee: Benedikt Ritter
>            Priority: Minor
>             Fix For: Review Patch, Discussion, 3.x
>
>         Attachments: LANG-823.patch, LANG-823.test.patch
>
>
> When a user issues a split with a delimiter and the string does not contain 
> the delimiter the result is normally an array with one item that contains the 
> content of the string.
> It seems strange that StringUtils does not behave consistently in the context 
> of an empty string.
> For example,
> {code}
> package maf.test;
> import junit.framework.TestCase;
> import org.apache.commons.lang.StringUtils;
> public class StringUtilsTest extends TestCase {
>       public void testStringUtils() {
>               // The following two lines work correctly  
>               assertTrue(StringUtils.split("x",",")[0].equals("x"));
>               assertTrue(StringUtils.split(" ",",")[0].equals(" "));
>               
>               // The following should also work but 
>               // in commons-lang-2.5.jar the test fails here
>               assertTrue(StringUtils.split("",",")[0].equals("")); 
>       }
> }
> {code}
> There seems to be no logic behind making split work differently in the case 
> of empty strings.  
> For the next release, I would suggest a behavior change for StringUtils this 
> will have side effects but would be more logically consistent.  
> Users who depend on the old behavior could stick with 2.5 release and/or 
> implement code in the caller to simulate the behavior.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to