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 3816a61f45..faf30f7b25 100644 --- a/core-java/src/main/java/com/baeldung/string/RemoveLastChar.java +++ b/core-java/src/main/java/com/baeldung/string/RemoveLastChar.java @@ -10,7 +10,7 @@ package com.baeldung.string; public class RemoveLastChar { public static String substring (String s) { if (s == null || s.length() == 0) { - return null; + return s; } else { return (s.substring(0, s.length() - 1)); } 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 80d76e96ac..a39212ced4 100644 --- a/core-java/src/test/java/com/baeldung/string/RemoveLastCharTest.java +++ b/core-java/src/test/java/com/baeldung/string/RemoveLastCharTest.java @@ -26,6 +26,7 @@ public class RemoveLastCharTest { public void givenTestString_substring_getStingWithoutLastChar() { Assert.assertEquals("abcde", RemoveLastChar.substring(TEST_STRING)); Assert.assertEquals("abcde", StringUtils.chop(TEST_STRING)); + Assert.assertEquals("abcde", StringUtils.substring(TEST_STRING, 0, TEST_STRING.length() - 1)); } @Test @@ -37,20 +38,25 @@ public class RemoveLastCharTest { @Test public void givenEmptyString_substring_getEmptyString() { - Assert.assertEquals(NULL_STRING, RemoveLastChar.substring(EMPTY_STRING)); - Assert.assertEquals(NULL_STRING, StringUtils.chop(NULL_STRING)); + Assert.assertEquals(EMPTY_STRING, RemoveLastChar.substring(EMPTY_STRING)); + Assert.assertEquals(EMPTY_STRING, StringUtils.chop(EMPTY_STRING)); + Assert.assertEquals(EMPTY_STRING, StringUtils.substring(EMPTY_STRING, 0, EMPTY_STRING.length() - 1)); + } @Test public void givenOneCharString_substring_getEmptyString() { Assert.assertEquals(EMPTY_STRING, RemoveLastChar.substring(ONE_CHAR_STRING)); Assert.assertEquals(EMPTY_STRING, StringUtils.chop(ONE_CHAR_STRING)); + Assert.assertEquals(EMPTY_STRING, StringUtils.substring(ONE_CHAR_STRING, 0, ONE_CHAR_STRING.length() - 1)); + } @Test public void givenStringWithWhiteSpaceAtTheEnd_substring_getStringWithoutWhiteSpaceAtTheEnd() { Assert.assertEquals("abc", RemoveLastChar.substring(WHITE_SPACE_AT_THE_END_STRING)); Assert.assertEquals("abc", StringUtils.chop(WHITE_SPACE_AT_THE_END_STRING)); + Assert.assertEquals("abc", StringUtils.substring(WHITE_SPACE_AT_THE_END_STRING, 0, WHITE_SPACE_AT_THE_END_STRING.length() - 1)); } }