diff --git a/core-java/src/main/java/com/baeldung/string/RemoveLastChar.java b/core-java/src/main/java/com/baeldung/string/RemoveLastChar.java new file mode 100644 index 0000000000..3816a61f45 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/string/RemoveLastChar.java @@ -0,0 +1,18 @@ +/** + * + */ +package com.baeldung.string; + +/** + * @author iaforek + * + */ +public class RemoveLastChar { + public static String substring (String s) { + if (s == null || s.length() == 0) { + return null; + } 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 new file mode 100644 index 0000000000..80d76e96ac --- /dev/null +++ b/core-java/src/test/java/com/baeldung/string/RemoveLastCharTest.java @@ -0,0 +1,56 @@ +/** + * + */ +package com.baeldung.string; + +import org.apache.commons.lang3.StringUtils; +import org.junit.Assert; +import org.junit.Test; + +/** + * @author iaforek + * + */ +public class RemoveLastCharTest { + + public static final String TEST_STRING = "abcdef"; + public static final String NULL_STRING = null; + 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 "; + + /** + * Test method for {@link com.baeldung.string.RemoveLastChar#substring(java.lang.String)}. + */ + @Test + public void givenTestString_substring_getStingWithoutLastChar() { + Assert.assertEquals("abcde", RemoveLastChar.substring(TEST_STRING)); + Assert.assertEquals("abcde", StringUtils.chop(TEST_STRING)); + } + + @Test + public void givenNullString_substring_getNullString() { + Assert.assertEquals(NULL_STRING, RemoveLastChar.substring(NULL_STRING)); + Assert.assertEquals(NULL_STRING, StringUtils.chop(NULL_STRING)); + + } + + @Test + public void givenEmptyString_substring_getEmptyString() { + Assert.assertEquals(NULL_STRING, RemoveLastChar.substring(EMPTY_STRING)); + Assert.assertEquals(NULL_STRING, StringUtils.chop(NULL_STRING)); + } + + @Test + public void givenOneCharString_substring_getEmptyString() { + Assert.assertEquals(EMPTY_STRING, RemoveLastChar.substring(ONE_CHAR_STRING)); + Assert.assertEquals(EMPTY_STRING, StringUtils.chop(ONE_CHAR_STRING)); + } + + @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)); + } + +}