Last active
March 5, 2018 03:31
-
-
Save asela38/f9bfcab049461ed3db765b64ff1b6757 to your computer and use it in GitHub Desktop.
If you have worked with flat files String.split should have been useful to you. One inconsistency, I found is change in behavior on cause of empty strings appeared in file with operator(",") and without separator.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.Assert.assertArrayEquals; | |
import static org.junit.Assert.assertFalse; | |
import static org.junit.Assert.assertTrue; | |
import java.util.Arrays; | |
import org.apache.commons.lang3.StringUtils; | |
import org.junit.Test; | |
public class SplitTest { | |
@Test | |
public void inconsistancyIObserved() throws Exception { | |
assertTrue("".split(",").length == 1); | |
assertTrue(",".split(",").length == 0); | |
assertTrue(",,".split(",").length == 0); | |
} | |
@Test | |
public void expectedConsistency() throws Exception { | |
assertTrue(Arrays.stream("".split(",")).filter(StringUtils::isNotBlank).toArray(String[]::new).length == 0); | |
assertTrue(Arrays.stream(",".split(",")).filter(StringUtils::isNotBlank).toArray(String[]::new).length == 0); | |
assertTrue(Arrays.stream(",,".split(",")).filter(StringUtils::isNotBlank).toArray(String[]::new).length == 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment