Skip to content

Commit 9fd2a91

Browse files
iaforekpedja4
authored andcommitted
BAEL-838 Re-adding last changes with regexp and new line strings. (eugenp#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.
1 parent e8e3234 commit 9fd2a91

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

core-java/src/main/java/com/baeldung/string/RemoveLastChar.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@ public static String substring(String s) {
88
return (s.substring(0, s.length() - 1));
99
}
1010
}
11+
12+
public static String chop(String s) {
13+
if (s == null) {
14+
return s;
15+
} else {
16+
return s.replaceAll(".$", "");
17+
}
18+
}
1119
}

core-java/src/test/java/com/baeldung/string/RemoveLastCharTest.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.baeldung.string;
22

33
import static org.junit.Assert.assertEquals;
4-
4+
import static org.junit.Assert.assertNotEquals;
55
import org.apache.commons.lang3.StringUtils;
66
import org.junit.Test;
77

@@ -12,38 +12,67 @@ public class RemoveLastCharTest {
1212
public static final String EMPTY_STRING = "";
1313
public static final String ONE_CHAR_STRING = "a";
1414
public static final String WHITE_SPACE_AT_THE_END_STRING = "abc ";
15+
public static final String NEW_LINE_AT_THE_END_STRING = "abc\n";
16+
public static final String MULTIPLE_LINES_STRING = "abc\ndef";
1517

1618
@Test
1719
public void givenTestString_whenSubstring_thenGetStingWithoutLastChar() {
1820
assertEquals("abcde", RemoveLastChar.substring(TEST_STRING));
1921
assertEquals("abcde", StringUtils.substring(TEST_STRING, 0, TEST_STRING.length() - 1));
2022
assertEquals("abcde", StringUtils.chop(TEST_STRING));
23+
assertEquals("abcde", TEST_STRING.replaceAll(".$", ""));
24+
assertEquals("abcde", RemoveLastChar.chop(TEST_STRING));
2125
}
2226

2327
@Test
2428
public void givenNullString_whenSubstring_thenGetNullString() {
2529
assertEquals(NULL_STRING, RemoveLastChar.substring(NULL_STRING));
2630
assertEquals(NULL_STRING, StringUtils.chop(NULL_STRING));
31+
assertEquals(NULL_STRING, RemoveLastChar.chop(NULL_STRING));
2732
}
2833

2934
@Test
3035
public void givenEmptyString_whenSubstring_thenGetEmptyString() {
3136
assertEquals(EMPTY_STRING, RemoveLastChar.substring(EMPTY_STRING));
3237
assertEquals(EMPTY_STRING, StringUtils.substring(EMPTY_STRING, 0, EMPTY_STRING.length() - 1));
3338
assertEquals(EMPTY_STRING, StringUtils.chop(EMPTY_STRING));
39+
assertEquals(EMPTY_STRING, EMPTY_STRING.replaceAll(".$", ""));
40+
assertEquals(EMPTY_STRING, RemoveLastChar.chop(EMPTY_STRING));
3441
}
3542

3643
@Test
3744
public void givenOneCharString_whenSubstring_thenGetEmptyString() {
3845
assertEquals(EMPTY_STRING, RemoveLastChar.substring(ONE_CHAR_STRING));
3946
assertEquals(EMPTY_STRING, StringUtils.substring(ONE_CHAR_STRING, 0, ONE_CHAR_STRING.length() - 1));
4047
assertEquals(EMPTY_STRING, StringUtils.chop(ONE_CHAR_STRING));
48+
assertEquals(EMPTY_STRING, ONE_CHAR_STRING.replaceAll(".$", ""));
49+
assertEquals(EMPTY_STRING, RemoveLastChar.chop(ONE_CHAR_STRING));
4150
}
4251

4352
@Test
4453
public void givenStringWithWhiteSpaceAtTheEnd_whenSubstring_thenGetStringWithoutWhiteSpaceAtTheEnd() {
4554
assertEquals("abc", RemoveLastChar.substring(WHITE_SPACE_AT_THE_END_STRING));
4655
assertEquals("abc", StringUtils.substring(WHITE_SPACE_AT_THE_END_STRING, 0, WHITE_SPACE_AT_THE_END_STRING.length() - 1));
4756
assertEquals("abc", StringUtils.chop(WHITE_SPACE_AT_THE_END_STRING));
57+
assertEquals("abc", WHITE_SPACE_AT_THE_END_STRING.replaceAll(".$", ""));
58+
assertEquals("abc", RemoveLastChar.chop(WHITE_SPACE_AT_THE_END_STRING));
59+
}
60+
61+
@Test
62+
public void givenStringWithNewLineAtTheEnd_whenSubstring_thenGetStringWithoutNewLine() {
63+
assertEquals("abc", RemoveLastChar.substring(NEW_LINE_AT_THE_END_STRING));
64+
assertEquals("abc", StringUtils.substring(NEW_LINE_AT_THE_END_STRING, 0, NEW_LINE_AT_THE_END_STRING.length() - 1));
65+
assertEquals("abc", StringUtils.chop(NEW_LINE_AT_THE_END_STRING));
66+
assertNotEquals("abc", NEW_LINE_AT_THE_END_STRING.replaceAll(".$", ""));
67+
assertNotEquals("abc", RemoveLastChar.chop(NEW_LINE_AT_THE_END_STRING));
68+
}
69+
70+
@Test
71+
public void givenMultiLineString_whenSubstring_thenGetStringWithoutNewLine() {
72+
assertEquals("abc\nde", RemoveLastChar.substring(MULTIPLE_LINES_STRING));
73+
assertEquals("abc\nde", StringUtils.substring(MULTIPLE_LINES_STRING, 0, MULTIPLE_LINES_STRING.length() - 1));
74+
assertEquals("abc\nde", StringUtils.chop(MULTIPLE_LINES_STRING));
75+
assertEquals("abc\nde", MULTIPLE_LINES_STRING.replaceAll(".$", ""));
76+
assertEquals("abc\nde", RemoveLastChar.chop(MULTIPLE_LINES_STRING));
4877
}
4978
}

0 commit comments

Comments
 (0)