Skip to content

Instantly share code, notes, and snippets.

@asela38
Last active March 5, 2018 03:31
Show Gist options
  • Save asela38/f9bfcab049461ed3db765b64ff1b6757 to your computer and use it in GitHub Desktop.
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.
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