Skip to content

Commit 6e58963

Browse files
iaforekpmaric
authored andcommitted
BAEL-838 Initial code showing how to remove last char - helper class and tests.
(cherry picked from commit d889dba)
1 parent c0164a3 commit 6e58963

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
*
3+
*/
4+
package com.baeldung.string;
5+
6+
/**
7+
* @author iaforek
8+
*
9+
*/
10+
public class RemoveLastChar {
11+
public static String substring (String s) {
12+
if (s == null || s.length() == 0) {
13+
return null;
14+
} else {
15+
return (s.substring(0, s.length() - 1));
16+
}
17+
}
18+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
*
3+
*/
4+
package com.baeldung.string;
5+
6+
import org.apache.commons.lang3.StringUtils;
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
/**
11+
* @author iaforek
12+
*
13+
*/
14+
public class RemoveLastCharTest {
15+
16+
public static final String TEST_STRING = "abcdef";
17+
public static final String NULL_STRING = null;
18+
public static final String EMPTY_STRING = "";
19+
public static final String ONE_CHAR_STRING = "a";
20+
public static final String WHITE_SPACE_AT_THE_END_STRING = "abc ";
21+
22+
/**
23+
* Test method for {@link com.baeldung.string.RemoveLastChar#substring(java.lang.String)}.
24+
*/
25+
@Test
26+
public void givenTestString_substring_getStingWithoutLastChar() {
27+
Assert.assertEquals("abcde", RemoveLastChar.substring(TEST_STRING));
28+
Assert.assertEquals("abcde", StringUtils.chop(TEST_STRING));
29+
}
30+
31+
@Test
32+
public void givenNullString_substring_getNullString() {
33+
Assert.assertEquals(NULL_STRING, RemoveLastChar.substring(NULL_STRING));
34+
Assert.assertEquals(NULL_STRING, StringUtils.chop(NULL_STRING));
35+
36+
}
37+
38+
@Test
39+
public void givenEmptyString_substring_getEmptyString() {
40+
Assert.assertEquals(NULL_STRING, RemoveLastChar.substring(EMPTY_STRING));
41+
Assert.assertEquals(NULL_STRING, StringUtils.chop(NULL_STRING));
42+
}
43+
44+
@Test
45+
public void givenOneCharString_substring_getEmptyString() {
46+
Assert.assertEquals(EMPTY_STRING, RemoveLastChar.substring(ONE_CHAR_STRING));
47+
Assert.assertEquals(EMPTY_STRING, StringUtils.chop(ONE_CHAR_STRING));
48+
}
49+
50+
@Test
51+
public void givenStringWithWhiteSpaceAtTheEnd_substring_getStringWithoutWhiteSpaceAtTheEnd() {
52+
Assert.assertEquals("abc", RemoveLastChar.substring(WHITE_SPACE_AT_THE_END_STRING));
53+
Assert.assertEquals("abc", StringUtils.chop(WHITE_SPACE_AT_THE_END_STRING));
54+
}
55+
56+
}

0 commit comments

Comments
 (0)