From 9fd2a911b11d966e75cda9d1ed1b4875d090e7db Mon Sep 17 00:00:00 2001 From: iaforek Date: Thu, 11 May 2017 11:22:34 +0100 Subject: [PATCH] BAEL-838 Re-adding last changes with regexp and new line strings. (#1825) * Code for Dependency Injection Article. * Added Java based configuration. Downloaded formatter.xml and reformatted all changed files. Manually changed tab into 4 spaces in XML configuration files. * BAEL-434 - Spring Roo project files generated by Spring Roo. No formatting applied. Added POM, java and resources folders. * Moved project from roo to spring-roo folder. * BAEL-838 Initial code showing how to remove last char - helper class and tests. * BAEL-838 Corrected Helper class and associated empty string test case. Added StringUtils.substing tests. * BAEL-838 Refromatted code using formatter.xml. Added Assert.assertEquals import. Renamed test to follow convention. Reordered tests. * BAEL-838 - Added regex method and updated tests. * BAEL-838 Added new line examples. --- .../com/baeldung/string/RemoveLastChar.java | 8 +++++ .../baeldung/string/RemoveLastCharTest.java | 31 ++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/core-java/src/main/java/com/baeldung/string/RemoveLastChar.java b/core-java/src/main/java/com/baeldung/string/RemoveLastChar.java index ee1d5c597d..8d15f0d7f5 100644 --- a/core-java/src/main/java/com/baeldung/string/RemoveLastChar.java +++ b/core-java/src/main/java/com/baeldung/string/RemoveLastChar.java @@ -8,4 +8,12 @@ public class RemoveLastChar { return (s.substring(0, s.length() - 1)); } } + + public static String chop(String s) { + if (s == null) { + return s; + } else { + return s.replaceAll(".$", ""); + } + } } diff --git a/core-java/src/test/java/com/baeldung/string/RemoveLastCharTest.java b/core-java/src/test/java/com/baeldung/string/RemoveLastCharTest.java index f1212b2ed9..29cf856da3 100644 --- a/core-java/src/test/java/com/baeldung/string/RemoveLastCharTest.java +++ b/core-java/src/test/java/com/baeldung/string/RemoveLastCharTest.java @@ -1,7 +1,7 @@ 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,18 +12,23 @@ 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() { assertEquals("abcde", RemoveLastChar.substring(TEST_STRING)); assertEquals("abcde", StringUtils.substring(TEST_STRING, 0, TEST_STRING.length() - 1)); assertEquals("abcde", StringUtils.chop(TEST_STRING)); + assertEquals("abcde", TEST_STRING.replaceAll(".$", "")); + assertEquals("abcde", RemoveLastChar.chop(TEST_STRING)); } @Test public void givenNullString_whenSubstring_thenGetNullString() { assertEquals(NULL_STRING, RemoveLastChar.substring(NULL_STRING)); assertEquals(NULL_STRING, StringUtils.chop(NULL_STRING)); + assertEquals(NULL_STRING, RemoveLastChar.chop(NULL_STRING)); } @Test @@ -31,6 +36,8 @@ public class RemoveLastCharTest { assertEquals(EMPTY_STRING, RemoveLastChar.substring(EMPTY_STRING)); assertEquals(EMPTY_STRING, StringUtils.substring(EMPTY_STRING, 0, EMPTY_STRING.length() - 1)); assertEquals(EMPTY_STRING, StringUtils.chop(EMPTY_STRING)); + assertEquals(EMPTY_STRING, EMPTY_STRING.replaceAll(".$", "")); + assertEquals(EMPTY_STRING, RemoveLastChar.chop(EMPTY_STRING)); } @Test @@ -38,6 +45,8 @@ public class RemoveLastCharTest { assertEquals(EMPTY_STRING, RemoveLastChar.substring(ONE_CHAR_STRING)); assertEquals(EMPTY_STRING, StringUtils.substring(ONE_CHAR_STRING, 0, ONE_CHAR_STRING.length() - 1)); assertEquals(EMPTY_STRING, StringUtils.chop(ONE_CHAR_STRING)); + assertEquals(EMPTY_STRING, ONE_CHAR_STRING.replaceAll(".$", "")); + assertEquals(EMPTY_STRING, RemoveLastChar.chop(ONE_CHAR_STRING)); } @Test @@ -45,5 +54,25 @@ public class RemoveLastCharTest { assertEquals("abc", RemoveLastChar.substring(WHITE_SPACE_AT_THE_END_STRING)); assertEquals("abc", StringUtils.substring(WHITE_SPACE_AT_THE_END_STRING, 0, WHITE_SPACE_AT_THE_END_STRING.length() - 1)); assertEquals("abc", StringUtils.chop(WHITE_SPACE_AT_THE_END_STRING)); + 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)); } }