BAEL-5550: Trim a string based on the string length

This commit is contained in:
Azhwani 2022-06-19 00:28:54 +02:00
parent 197cd508e0
commit d46370d88f
2 changed files with 158 additions and 0 deletions

View File

@ -0,0 +1,105 @@
package com.baeldung.truncate;
import java.util.Optional;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import com.google.common.base.Splitter;
public class TruncateString {
private static final String EMPTY = "";
public static String usingSubstringMethod(String text, int length) {
if (length < 0) {
throw new IllegalArgumentException("length cannot be negative");
}
if (text == null) {
return EMPTY;
}
if (text.length() <= length) {
return text;
} else {
return text.substring(0, length);
}
}
public static String usingSplitMethod(String text, int length) {
if (length < 0) {
throw new IllegalArgumentException("length cannot be negative");
}
if (text == null) {
return EMPTY;
}
String[] results = text.split("(?<=\\G.{" + length + "})");
return results[0];
}
public static String usingPattern(String text, int length) {
if (length < 0) {
throw new IllegalArgumentException("length cannot be negative");
}
if (text == null) {
return EMPTY;
}
Optional<String> result = Pattern.compile(".{1," + length + "}")
.matcher(text)
.results()
.map(MatchResult::group)
.findFirst();
return result.isPresent() ? result.get() : EMPTY;
}
public static String usingCodePointsMethod(String text, int length) {
if (length < 0) {
throw new IllegalArgumentException("length cannot be negative");
}
if (text == null) {
return EMPTY;
}
return text.codePoints()
.limit(length)
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
.toString();
}
public static String usingLeftMethod(String text, int length) {
return StringUtils.left(text, length);
}
public static String usingTruncateMethod(String text, int length) {
return StringUtils.truncate(text, length);
}
public static String usingSplitter(String text, int length) {
if (length < 0) {
throw new IllegalArgumentException("length cannot be negative");
}
if (text == null) {
return EMPTY;
}
Iterable<String> parts = Splitter.fixedLength(length)
.split(text);
return parts.iterator()
.next();
}
}

View File

@ -0,0 +1,53 @@
package com.baeldung.truncate;
import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.Test;
public class TruncateStringUnitTest {
private static final String TEXT = "Welcome to baeldung.com";
@Test
public void givenStringAndLength_whenUsingSubstringMethod_thenTruncate() {
assertEquals(TruncateString.usingSubstringMethod(TEXT, 10), "Welcome to");
}
@Test
public void givenStringAndLength_whenUsingSplitMethod_thenTruncate() {
assertEquals(TruncateString.usingSplitMethod(TEXT, 13), "Welcome to ba");
}
@Test
public void givenStringAndLength_whenUsingPattern_thenTruncate() {
assertEquals(TruncateString.usingPattern(TEXT, 19), "Welcome to baeldung");
}
@Test
public void givenStringAndLength_whenUsingCodePointsMethod_thenTruncate() {
assertEquals(TruncateString.usingCodePointsMethod(TEXT, 6), "Welcom");
}
@Test
public void givenStringAndLength_whenUsingLeftMethod_thenTruncate() {
assertEquals(TruncateString.usingLeftMethod(TEXT, 15), "Welcome to bael");
}
@Test
public void givenStringAndLength_whenUsingTruncateMethod_thenTruncate() {
assertEquals(TruncateString.usingTruncateMethod(TEXT, 20), "Welcome to baeldung.");
}
@Test
public void givenStringAndLength_whenUsingSplitter_thenTruncate() {
assertEquals(TruncateString.usingSplitter(TEXT, 3), "Wel");
}
}