BAEL-4702

- Code snippets for split Java String by New line
This commit is contained in:
unknown 2021-02-20 21:08:27 +05:30
parent 23e368fc28
commit d1bbd3502b
1 changed files with 27 additions and 0 deletions

View File

@ -0,0 +1,27 @@
package com.baeldung.splitstringbynewline;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class SplitStringByNewLineUnitTest {
@Test
public void givenString_whenSplitByNewLineUsingRegularExpressionPattern_thenReturnsArray() {
assertThat("Line1\nLine2\nLine3".split("\\r?\\n|\\r")).containsExactly("Line1", "Line2", "Line3");
assertThat("Line1\rLine2\rLine3".split("\\r?\\n|\\r")).containsExactly("Line1", "Line2", "Line3");
assertThat("Line1\r\nLine2\r\nLine3".split("\\r?\\n|\\r")).containsExactly("Line1", "Line2", "Line3");
}
@Test
public void givenString_whenSplitByNewLineUsingJava8Pattern_thenReturnsArray() {
assertThat("Line1\nLine2\nLine3".split("\\R")).containsExactly("Line1", "Line2", "Line3");
assertThat("Line1\rLine2\rLine3".split("\\R")).containsExactly("Line1", "Line2", "Line3");
assertThat("Line1\r\nLine2\r\nLine3".split("\\R")).containsExactly("Line1", "Line2", "Line3");
}
}