BAEL-838 Renamed RemoveLastChar class to StringHelper and added Java8 examples. Refactord code.
This commit is contained in:
parent
37da5f8854
commit
1af7196a65
@ -1,19 +0,0 @@
|
||||
package com.baeldung.string;
|
||||
|
||||
public class RemoveLastChar {
|
||||
public static String substring(String s) {
|
||||
if (s == null || s.length() == 0) {
|
||||
return s;
|
||||
} else {
|
||||
return (s.substring(0, s.length() - 1));
|
||||
}
|
||||
}
|
||||
|
||||
public static String chop(String s) {
|
||||
if (s == null) {
|
||||
return s;
|
||||
} else {
|
||||
return s.replaceAll(".$", "");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.baeldung.string;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class StringHelper {
|
||||
public static String withoutLastChar(String s) {
|
||||
return (s == null || s.length() == 0) ? s : (s.substring(0, s.length() - 1));
|
||||
}
|
||||
|
||||
public static String removeLastChar(String s) {
|
||||
return (s == null) ? s : s.replaceAll(".$", "");
|
||||
}
|
||||
|
||||
public static String withoutLastCharacter(String s) {
|
||||
return Optional.ofNullable(s)
|
||||
.filter(str -> str.length() != 0)
|
||||
.map(str -> str.substring(0, str.length() - 1))
|
||||
.orElse(s);
|
||||
}
|
||||
|
||||
public static String removeLastCharacter(String s) {
|
||||
return Optional.ofNullable(s)
|
||||
.map(str -> str.replaceAll(".$", ""))
|
||||
.orElse(null);
|
||||
}
|
||||
}
|
@ -5,7 +5,7 @@ import static org.junit.Assert.assertNotEquals;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.Test;
|
||||
|
||||
public class RemoveLastCharTest {
|
||||
public class StringHelperTest {
|
||||
|
||||
public static final String TEST_STRING = "abcdef";
|
||||
public static final String NULL_STRING = null;
|
||||
@ -17,62 +17,76 @@ public class RemoveLastCharTest {
|
||||
|
||||
@Test
|
||||
public void givenTestString_whenSubstring_thenGetStingWithoutLastChar() {
|
||||
assertEquals("abcde", RemoveLastChar.substring(TEST_STRING));
|
||||
assertEquals("abcde", StringHelper.withoutLastChar(TEST_STRING));
|
||||
assertEquals("abcde", StringUtils.substring(TEST_STRING, 0, TEST_STRING.length() - 1));
|
||||
assertEquals("abcde", StringUtils.chop(TEST_STRING));
|
||||
assertEquals("abcde", TEST_STRING.replaceAll(".$", ""));
|
||||
assertEquals("abcde", RemoveLastChar.chop(TEST_STRING));
|
||||
assertEquals("abcde", StringHelper.removeLastChar(TEST_STRING));
|
||||
assertEquals("abcde", StringHelper.withoutLastCharacter(TEST_STRING));
|
||||
assertEquals("abcde", StringHelper.removeLastCharacter(TEST_STRING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNullString_whenSubstring_thenGetNullString() {
|
||||
assertEquals(NULL_STRING, RemoveLastChar.substring(NULL_STRING));
|
||||
assertEquals(NULL_STRING, StringHelper.withoutLastChar(NULL_STRING));
|
||||
assertEquals(NULL_STRING, StringUtils.chop(NULL_STRING));
|
||||
assertEquals(NULL_STRING, RemoveLastChar.chop(NULL_STRING));
|
||||
assertEquals(NULL_STRING, StringHelper.removeLastChar(NULL_STRING));
|
||||
assertEquals(NULL_STRING, StringHelper.withoutLastCharacter(NULL_STRING));
|
||||
assertEquals(NULL_STRING, StringHelper.removeLastCharacter(NULL_STRING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyString_whenSubstring_thenGetEmptyString() {
|
||||
assertEquals(EMPTY_STRING, RemoveLastChar.substring(EMPTY_STRING));
|
||||
assertEquals(EMPTY_STRING, StringHelper.withoutLastChar(EMPTY_STRING));
|
||||
assertEquals(EMPTY_STRING, StringUtils.substring(EMPTY_STRING, 0, EMPTY_STRING.length() - 1));
|
||||
assertEquals(EMPTY_STRING, StringUtils.chop(EMPTY_STRING));
|
||||
assertEquals(EMPTY_STRING, EMPTY_STRING.replaceAll(".$", ""));
|
||||
assertEquals(EMPTY_STRING, RemoveLastChar.chop(EMPTY_STRING));
|
||||
assertEquals(EMPTY_STRING, StringHelper.removeLastChar(EMPTY_STRING));
|
||||
assertEquals(EMPTY_STRING, StringHelper.withoutLastCharacter(EMPTY_STRING));
|
||||
assertEquals(EMPTY_STRING, StringHelper.removeLastCharacter(EMPTY_STRING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenOneCharString_whenSubstring_thenGetEmptyString() {
|
||||
assertEquals(EMPTY_STRING, RemoveLastChar.substring(ONE_CHAR_STRING));
|
||||
assertEquals(EMPTY_STRING, StringHelper.withoutLastChar(ONE_CHAR_STRING));
|
||||
assertEquals(EMPTY_STRING, StringUtils.substring(ONE_CHAR_STRING, 0, ONE_CHAR_STRING.length() - 1));
|
||||
assertEquals(EMPTY_STRING, StringUtils.chop(ONE_CHAR_STRING));
|
||||
assertEquals(EMPTY_STRING, ONE_CHAR_STRING.replaceAll(".$", ""));
|
||||
assertEquals(EMPTY_STRING, RemoveLastChar.chop(ONE_CHAR_STRING));
|
||||
assertEquals(EMPTY_STRING, StringHelper.removeLastChar(ONE_CHAR_STRING));
|
||||
assertEquals(EMPTY_STRING, StringHelper.withoutLastCharacter(ONE_CHAR_STRING));
|
||||
assertEquals(EMPTY_STRING, StringHelper.removeLastCharacter(ONE_CHAR_STRING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringWithWhiteSpaceAtTheEnd_whenSubstring_thenGetStringWithoutWhiteSpaceAtTheEnd() {
|
||||
assertEquals("abc", RemoveLastChar.substring(WHITE_SPACE_AT_THE_END_STRING));
|
||||
assertEquals("abc", StringHelper.withoutLastChar(WHITE_SPACE_AT_THE_END_STRING));
|
||||
assertEquals("abc", StringUtils.substring(WHITE_SPACE_AT_THE_END_STRING, 0, WHITE_SPACE_AT_THE_END_STRING.length() - 1));
|
||||
assertEquals("abc", StringUtils.chop(WHITE_SPACE_AT_THE_END_STRING));
|
||||
assertEquals("abc", WHITE_SPACE_AT_THE_END_STRING.replaceAll(".$", ""));
|
||||
assertEquals("abc", RemoveLastChar.chop(WHITE_SPACE_AT_THE_END_STRING));
|
||||
assertEquals("abc", StringHelper.removeLastChar(WHITE_SPACE_AT_THE_END_STRING));
|
||||
assertEquals("abc", StringHelper.withoutLastCharacter(WHITE_SPACE_AT_THE_END_STRING));
|
||||
assertEquals("abc", StringHelper.removeLastCharacter(WHITE_SPACE_AT_THE_END_STRING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringWithNewLineAtTheEnd_whenSubstring_thenGetStringWithoutNewLine() {
|
||||
assertEquals("abc", RemoveLastChar.substring(NEW_LINE_AT_THE_END_STRING));
|
||||
assertEquals("abc", StringHelper.withoutLastChar(NEW_LINE_AT_THE_END_STRING));
|
||||
assertEquals("abc", StringUtils.substring(NEW_LINE_AT_THE_END_STRING, 0, NEW_LINE_AT_THE_END_STRING.length() - 1));
|
||||
assertEquals("abc", StringUtils.chop(NEW_LINE_AT_THE_END_STRING));
|
||||
assertNotEquals("abc", NEW_LINE_AT_THE_END_STRING.replaceAll(".$", ""));
|
||||
assertNotEquals("abc", RemoveLastChar.chop(NEW_LINE_AT_THE_END_STRING));
|
||||
assertNotEquals("abc", StringHelper.removeLastChar(NEW_LINE_AT_THE_END_STRING));
|
||||
assertEquals("abc", StringHelper.withoutLastCharacter(NEW_LINE_AT_THE_END_STRING));
|
||||
assertNotEquals("abc", StringHelper.removeLastCharacter(NEW_LINE_AT_THE_END_STRING));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMultiLineString_whenSubstring_thenGetStringWithoutNewLine() {
|
||||
assertEquals("abc\nde", RemoveLastChar.substring(MULTIPLE_LINES_STRING));
|
||||
assertEquals("abc\nde", StringHelper.withoutLastChar(MULTIPLE_LINES_STRING));
|
||||
assertEquals("abc\nde", StringUtils.substring(MULTIPLE_LINES_STRING, 0, MULTIPLE_LINES_STRING.length() - 1));
|
||||
assertEquals("abc\nde", StringUtils.chop(MULTIPLE_LINES_STRING));
|
||||
assertEquals("abc\nde", MULTIPLE_LINES_STRING.replaceAll(".$", ""));
|
||||
assertEquals("abc\nde", RemoveLastChar.chop(MULTIPLE_LINES_STRING));
|
||||
assertEquals("abc\nde", StringHelper.removeLastChar(MULTIPLE_LINES_STRING));
|
||||
assertEquals("abc\nde", StringHelper.withoutLastCharacter(MULTIPLE_LINES_STRING));
|
||||
assertEquals("abc\nde", StringHelper.removeLastCharacter(MULTIPLE_LINES_STRING));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user