diff --git a/core-java-modules/core-java-string-operations-4/src/main/java/com/baeldung/truncate/TruncateString.java b/core-java-modules/core-java-string-operations-4/src/main/java/com/baeldung/truncate/TruncateString.java new file mode 100644 index 0000000000..788a180f19 --- /dev/null +++ b/core-java-modules/core-java-string-operations-4/src/main/java/com/baeldung/truncate/TruncateString.java @@ -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 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 parts = Splitter.fixedLength(length) + .split(text); + + return parts.iterator() + .next(); + } + +} diff --git a/core-java-modules/core-java-string-operations-4/src/test/java/com/baeldung/truncate/TruncateStringUnitTest.java b/core-java-modules/core-java-string-operations-4/src/test/java/com/baeldung/truncate/TruncateStringUnitTest.java new file mode 100644 index 0000000000..fd44e37546 --- /dev/null +++ b/core-java-modules/core-java-string-operations-4/src/test/java/com/baeldung/truncate/TruncateStringUnitTest.java @@ -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"); + } + +}