BAEL-5614 Remove Last Char from String Builder (#12577)
* BAEL-5614 Remove Last Char from String Builder - Added initial code with main - Added initial tests for each method going to be written on * Update RemoveLastChar.java * Update and rename RemoveLastCharTest.java to RemoveLastCharUnitTest.java Co-authored-by: Grzegorz Piwowarek <gpiwowarek@gmail.com>
This commit is contained in:
parent
a05517302d
commit
d07ff5878c
|
@ -0,0 +1,67 @@
|
|||
package com.baeldung.stringbuilder;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
public class RemoveLastChar {
|
||||
static List<String> technologies = List.of("Spring Boot", "Quarkus", "Micronaut");
|
||||
|
||||
public static StringBuilder joinStringsWithLastCharAsDelimiter() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for (String tech : technologies) {
|
||||
sb.append(tech)
|
||||
.append(",");
|
||||
}
|
||||
|
||||
return sb;
|
||||
}
|
||||
|
||||
public static StringBuilder removeLastCharWithDeleteLastChar(StringBuilder sb) {
|
||||
|
||||
if (sb.length() == 0) {
|
||||
return sb;
|
||||
}
|
||||
|
||||
return sb.deleteCharAt(sb.length() - 1);
|
||||
}
|
||||
|
||||
public static StringBuilder removeLastCharWithSetLength(StringBuilder sb) {
|
||||
|
||||
if (sb.length() == 0) {
|
||||
return sb;
|
||||
}
|
||||
|
||||
sb.setLength(sb.length() - 1);
|
||||
return sb;
|
||||
}
|
||||
|
||||
public static StringBuilder joinStringsWithoutLastCharAsDelimiter() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String prefix = "";
|
||||
|
||||
for (String tech : technologies) {
|
||||
sb.append(prefix);
|
||||
prefix = ",";
|
||||
sb.append(tech);
|
||||
}
|
||||
|
||||
return sb;
|
||||
}
|
||||
|
||||
public static String joinStringsUsingJoin() {
|
||||
return String.join(",", technologies);
|
||||
}
|
||||
|
||||
public static String joinUsingStringJoiner() {
|
||||
StringJoiner joiner = new StringJoiner(",");
|
||||
technologies.forEach(joiner::add);
|
||||
return joiner.toString();
|
||||
}
|
||||
|
||||
public static String joinUsingStringUtils() {
|
||||
return StringUtils.join(technologies, ",");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
package com.baeldung.stringbuilder;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class RemoveLastCharUnitTest {
|
||||
|
||||
private static final String STRING_WITH_CHAR = "Spring Boot,Quarkus,Micronaut,";
|
||||
private static final String STRING_WITHOUT_CHAR = "Spring Boot,Quarkus,Micronaut";
|
||||
|
||||
@Test
|
||||
void givenCharAtEndOfString_whenRemoveLastCharWithDeleteLastChar_thenLastCharRemoved() {
|
||||
StringBuilder sb = RemoveLastChar.joinStringsWithLastCharAsDelimiter();
|
||||
Assertions.assertEquals(STRING_WITH_CHAR, sb.toString());
|
||||
|
||||
RemoveLastChar.removeLastCharWithDeleteLastChar(sb);
|
||||
Assertions.assertEquals(STRING_WITHOUT_CHAR, sb.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenCharAtEndOfString_whenRemoveLastCharWithSetLength_thenLastCharRemoved() {
|
||||
StringBuilder sb = RemoveLastChar.joinStringsWithLastCharAsDelimiter();
|
||||
Assertions.assertEquals(STRING_WITH_CHAR, sb.toString());
|
||||
|
||||
RemoveLastChar.removeLastCharWithSetLength(sb);
|
||||
Assertions.assertEquals(STRING_WITHOUT_CHAR, sb.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenForLoopJoinWithPrefix_thenLastCharIsNotPresent() {
|
||||
Assertions.assertEquals(STRING_WITHOUT_CHAR, RemoveLastChar.joinStringsWithoutLastCharAsDelimiter()
|
||||
.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenJoiningStringsUsingJoin_thenLastCharIsNotPresent() {
|
||||
Assertions.assertEquals(STRING_WITHOUT_CHAR, RemoveLastChar.joinStringsUsingJoin());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenJoiningStringsUsingStringJoiner_thenLastCharIsNotPresent() {
|
||||
Assertions.assertEquals(STRING_WITHOUT_CHAR, RemoveLastChar.joinUsingStringJoiner());
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenJoiningStringsUsingStringUtils_thenLastCharIsNotPresent() {
|
||||
Assertions.assertEquals(STRING_WITHOUT_CHAR, RemoveLastChar.joinUsingStringUtils());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue