Added new line examples.
package com.baeldung.string;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
@@ -12,6 +13,8 @@ public class RemoveLastCharTest {
public static final String EMPTY_STRING = "";
public static final String ONE_CHAR_STRING = "a";
public static final String WHITE_SPACE_AT_THE_END_STRING = "abc ";
+ public static final String NEW_LINE_AT_THE_END_STRING = "abc\n";
+ public static final String MULTIPLE_LINES_STRING = "abc\ndef";
@Test
public void givenTestString_whenSubstring_thenGetStingWithoutLastChar() {
@@ -55,4 +58,22 @@ public class RemoveLastCharTest {
assertEquals("abc", WHITE_SPACE_AT_THE_END_STRING.replaceAll(".$", ""));
assertEquals("abc", RemoveLastChar.chop(WHITE_SPACE_AT_THE_END_STRING));
}
+
+ @Test
+ public void givenStringWithNewLineAtTheEnd_whenSubstring_thenGetStringWithoutNewLine() {
+ assertEquals("abc", RemoveLastChar.substring(NEW_LINE_AT_THE_END_STRING));
+ assertEquals("abc", StringUtils.substring(NEW_LINE_AT_THE_END_STRING, 0, NEW_LINE_AT_THE_END_STRING.length() - 1));
+ assertEquals("abc", StringUtils.chop(NEW_LINE_AT_THE_END_STRING));
+ assertNotEquals("abc", NEW_LINE_AT_THE_END_STRING.replaceAll(".$", ""));
+ assertNotEquals("abc", RemoveLastChar.chop(NEW_LINE_AT_THE_END_STRING));
+ }
+
+ @Test
+ public void givenMultiLineString_whenSubstring_thenGetStringWithoutNewLine() {
+ assertEquals("abc\nde", RemoveLastChar.substring(MULTIPLE_LINES_STRING));
+ assertEquals("abc\nde", StringUtils.substring(MULTIPLE_LINES_STRING, 0, MULTIPLE_LINES_STRING.length() - 1));
+ assertEquals("abc\nde", StringUtils.chop(MULTIPLE_LINES_STRING));
+ assertEquals("abc\nde", MULTIPLE_LINES_STRING.replaceAll(".$", ""));
+ assertEquals("abc\nde", RemoveLastChar.chop(MULTIPLE_LINES_STRING));
+ }
}
