Skip to content

Commit 075eeb1

Browse files
iaforekpedja4
authored andcommitted
BAEL-838 Refactord code with Java8 examples. (eugenp#1833)
* 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. * BAEL-838 Renamed RemoveLastChar class to StringHelper and added Java8 examples. Refactord code. * BAEL-838 Changed method names
1 parent d172d2d commit 075eeb1

3 files changed

Lines changed: 55 additions & 34 deletions

File tree

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

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.string;
2+
3+
import java.util.Optional;
4+
5+
public class StringHelper {
6+
public static String removeLastChar(String s) {
7+
return (s == null || s.length() == 0) ? s : (s.substring(0, s.length() - 1));
8+
}
9+
10+
public static String removeLastCharRegex(String s) {
11+
return (s == null) ? s : s.replaceAll(".$", "");
12+
}
13+
14+
public static String removeLastCharOptional(String s) {
15+
return Optional.ofNullable(s)
16+
.filter(str -> str.length() != 0)
17+
.map(str -> str.substring(0, str.length() - 1))
18+
.orElse(s);
19+
}
20+
21+
public static String removeLastCharRegexOptional(String s) {
22+
return Optional.ofNullable(s)
23+
.map(str -> str.replaceAll(".$", ""))
24+
.orElse(null);
25+
}
26+
}

core-java/src/test/java/com/baeldung/string/RemoveLastCharTest.java renamed to core-java/src/test/java/com/baeldung/string/StringHelperTest.java

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import org.apache.commons.lang3.StringUtils;
66
import org.junit.Test;
77

8-
public class RemoveLastCharTest {
8+
public class StringHelperTest {
99

1010
public static final String TEST_STRING = "abcdef";
1111
public static final String NULL_STRING = null;
@@ -17,62 +17,76 @@ public class RemoveLastCharTest {
1717

1818
@Test
1919
public void givenTestString_whenSubstring_thenGetStingWithoutLastChar() {
20-
assertEquals("abcde", RemoveLastChar.substring(TEST_STRING));
20+
assertEquals("abcde", StringHelper.removeLastChar(TEST_STRING));
2121
assertEquals("abcde", StringUtils.substring(TEST_STRING, 0, TEST_STRING.length() - 1));
2222
assertEquals("abcde", StringUtils.chop(TEST_STRING));
2323
assertEquals("abcde", TEST_STRING.replaceAll(".$", ""));
24-
assertEquals("abcde", RemoveLastChar.chop(TEST_STRING));
24+
assertEquals("abcde", StringHelper.removeLastCharRegex(TEST_STRING));
25+
assertEquals("abcde", StringHelper.removeLastCharOptional(TEST_STRING));
26+
assertEquals("abcde", StringHelper.removeLastCharRegexOptional(TEST_STRING));
2527
}
2628

2729
@Test
2830
public void givenNullString_whenSubstring_thenGetNullString() {
29-
assertEquals(NULL_STRING, RemoveLastChar.substring(NULL_STRING));
31+
assertEquals(NULL_STRING, StringHelper.removeLastChar(NULL_STRING));
3032
assertEquals(NULL_STRING, StringUtils.chop(NULL_STRING));
31-
assertEquals(NULL_STRING, RemoveLastChar.chop(NULL_STRING));
33+
assertEquals(NULL_STRING, StringHelper.removeLastCharRegex(NULL_STRING));
34+
assertEquals(NULL_STRING, StringHelper.removeLastCharOptional(NULL_STRING));
35+
assertEquals(NULL_STRING, StringHelper.removeLastCharRegexOptional(NULL_STRING));
3236
}
3337

3438
@Test
3539
public void givenEmptyString_whenSubstring_thenGetEmptyString() {
36-
assertEquals(EMPTY_STRING, RemoveLastChar.substring(EMPTY_STRING));
40+
assertEquals(EMPTY_STRING, StringHelper.removeLastChar(EMPTY_STRING));
3741
assertEquals(EMPTY_STRING, StringUtils.substring(EMPTY_STRING, 0, EMPTY_STRING.length() - 1));
3842
assertEquals(EMPTY_STRING, StringUtils.chop(EMPTY_STRING));
3943
assertEquals(EMPTY_STRING, EMPTY_STRING.replaceAll(".$", ""));
40-
assertEquals(EMPTY_STRING, RemoveLastChar.chop(EMPTY_STRING));
44+
assertEquals(EMPTY_STRING, StringHelper.removeLastCharRegex(EMPTY_STRING));
45+
assertEquals(EMPTY_STRING, StringHelper.removeLastCharOptional(EMPTY_STRING));
46+
assertEquals(EMPTY_STRING, StringHelper.removeLastCharRegexOptional(EMPTY_STRING));
4147
}
4248

4349
@Test
4450
public void givenOneCharString_whenSubstring_thenGetEmptyString() {
45-
assertEquals(EMPTY_STRING, RemoveLastChar.substring(ONE_CHAR_STRING));
51+
assertEquals(EMPTY_STRING, StringHelper.removeLastChar(ONE_CHAR_STRING));
4652
assertEquals(EMPTY_STRING, StringUtils.substring(ONE_CHAR_STRING, 0, ONE_CHAR_STRING.length() - 1));
4753
assertEquals(EMPTY_STRING, StringUtils.chop(ONE_CHAR_STRING));
4854
assertEquals(EMPTY_STRING, ONE_CHAR_STRING.replaceAll(".$", ""));
49-
assertEquals(EMPTY_STRING, RemoveLastChar.chop(ONE_CHAR_STRING));
55+
assertEquals(EMPTY_STRING, StringHelper.removeLastCharRegex(ONE_CHAR_STRING));
56+
assertEquals(EMPTY_STRING, StringHelper.removeLastCharOptional(ONE_CHAR_STRING));
57+
assertEquals(EMPTY_STRING, StringHelper.removeLastCharRegexOptional(ONE_CHAR_STRING));
5058
}
5159

5260
@Test
5361
public void givenStringWithWhiteSpaceAtTheEnd_whenSubstring_thenGetStringWithoutWhiteSpaceAtTheEnd() {
54-
assertEquals("abc", RemoveLastChar.substring(WHITE_SPACE_AT_THE_END_STRING));
62+
assertEquals("abc", StringHelper.removeLastChar(WHITE_SPACE_AT_THE_END_STRING));
5563
assertEquals("abc", StringUtils.substring(WHITE_SPACE_AT_THE_END_STRING, 0, WHITE_SPACE_AT_THE_END_STRING.length() - 1));
5664
assertEquals("abc", StringUtils.chop(WHITE_SPACE_AT_THE_END_STRING));
5765
assertEquals("abc", WHITE_SPACE_AT_THE_END_STRING.replaceAll(".$", ""));
58-
assertEquals("abc", RemoveLastChar.chop(WHITE_SPACE_AT_THE_END_STRING));
66+
assertEquals("abc", StringHelper.removeLastCharRegex(WHITE_SPACE_AT_THE_END_STRING));
67+
assertEquals("abc", StringHelper.removeLastCharOptional(WHITE_SPACE_AT_THE_END_STRING));
68+
assertEquals("abc", StringHelper.removeLastCharRegexOptional(WHITE_SPACE_AT_THE_END_STRING));
5969
}
6070

6171
@Test
6272
public void givenStringWithNewLineAtTheEnd_whenSubstring_thenGetStringWithoutNewLine() {
63-
assertEquals("abc", RemoveLastChar.substring(NEW_LINE_AT_THE_END_STRING));
73+
assertEquals("abc", StringHelper.removeLastChar(NEW_LINE_AT_THE_END_STRING));
6474
assertEquals("abc", StringUtils.substring(NEW_LINE_AT_THE_END_STRING, 0, NEW_LINE_AT_THE_END_STRING.length() - 1));
6575
assertEquals("abc", StringUtils.chop(NEW_LINE_AT_THE_END_STRING));
6676
assertNotEquals("abc", NEW_LINE_AT_THE_END_STRING.replaceAll(".$", ""));
67-
assertNotEquals("abc", RemoveLastChar.chop(NEW_LINE_AT_THE_END_STRING));
77+
assertNotEquals("abc", StringHelper.removeLastCharRegex(NEW_LINE_AT_THE_END_STRING));
78+
assertEquals("abc", StringHelper.removeLastCharOptional(NEW_LINE_AT_THE_END_STRING));
79+
assertNotEquals("abc", StringHelper.removeLastCharRegexOptional(NEW_LINE_AT_THE_END_STRING));
6880
}
6981

7082
@Test
7183
public void givenMultiLineString_whenSubstring_thenGetStringWithoutNewLine() {
72-
assertEquals("abc\nde", RemoveLastChar.substring(MULTIPLE_LINES_STRING));
84+
assertEquals("abc\nde", StringHelper.removeLastChar(MULTIPLE_LINES_STRING));
7385
assertEquals("abc\nde", StringUtils.substring(MULTIPLE_LINES_STRING, 0, MULTIPLE_LINES_STRING.length() - 1));
7486
assertEquals("abc\nde", StringUtils.chop(MULTIPLE_LINES_STRING));
7587
assertEquals("abc\nde", MULTIPLE_LINES_STRING.replaceAll(".$", ""));
76-
assertEquals("abc\nde", RemoveLastChar.chop(MULTIPLE_LINES_STRING));
88+
assertEquals("abc\nde", StringHelper.removeLastCharRegex(MULTIPLE_LINES_STRING));
89+
assertEquals("abc\nde", StringHelper.removeLastCharOptional(MULTIPLE_LINES_STRING));
90+
assertEquals("abc\nde", StringHelper.removeLastCharRegexOptional(MULTIPLE_LINES_STRING));
7791
}
7892
}

0 commit comments

Comments
 (0)