|
| 1 | +package com.baeldung.truncate; |
| 2 | + |
| 3 | +import java.util.Optional; |
| 4 | +import java.util.regex.MatchResult; |
| 5 | +import java.util.regex.Pattern; |
| 6 | + |
| 7 | +import org.apache.commons.lang3.StringUtils; |
| 8 | + |
| 9 | +import com.google.common.base.Splitter; |
| 10 | + |
| 11 | +public class TruncateString { |
| 12 | + |
| 13 | + private static final String EMPTY = ""; |
| 14 | + |
| 15 | + public static String usingSubstringMethod(String text, int length) { |
| 16 | + if (length < 0) { |
| 17 | + throw new IllegalArgumentException("length cannot be negative"); |
| 18 | + } |
| 19 | + |
| 20 | + if (text == null) { |
| 21 | + return EMPTY; |
| 22 | + } |
| 23 | + |
| 24 | + if (text.length() <= length) { |
| 25 | + return text; |
| 26 | + } else { |
| 27 | + return text.substring(0, length); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + public static String usingSplitMethod(String text, int length) { |
| 32 | + if (length < 0) { |
| 33 | + throw new IllegalArgumentException("length cannot be negative"); |
| 34 | + } |
| 35 | + |
| 36 | + if (text == null) { |
| 37 | + return EMPTY; |
| 38 | + } |
| 39 | + |
| 40 | + String[] results = text.split("(?<=\\G.{" + length + "})"); |
| 41 | + |
| 42 | + return results[0]; |
| 43 | + } |
| 44 | + |
| 45 | + public static String usingPattern(String text, int length) { |
| 46 | + if (length < 0) { |
| 47 | + throw new IllegalArgumentException("length cannot be negative"); |
| 48 | + } |
| 49 | + |
| 50 | + if (text == null) { |
| 51 | + return EMPTY; |
| 52 | + } |
| 53 | + |
| 54 | + Optional<String> result = Pattern.compile(".{1," + length + "}") |
| 55 | + .matcher(text) |
| 56 | + .results() |
| 57 | + .map(MatchResult::group) |
| 58 | + .findFirst(); |
| 59 | + |
| 60 | + return result.isPresent() ? result.get() : EMPTY; |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + public static String usingCodePointsMethod(String text, int length) { |
| 65 | + if (length < 0) { |
| 66 | + throw new IllegalArgumentException("length cannot be negative"); |
| 67 | + } |
| 68 | + |
| 69 | + if (text == null) { |
| 70 | + return EMPTY; |
| 71 | + } |
| 72 | + |
| 73 | + return text.codePoints() |
| 74 | + .limit(length) |
| 75 | + .collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append) |
| 76 | + .toString(); |
| 77 | + } |
| 78 | + |
| 79 | + public static String usingLeftMethod(String text, int length) { |
| 80 | + |
| 81 | + return StringUtils.left(text, length); |
| 82 | + } |
| 83 | + |
| 84 | + public static String usingTruncateMethod(String text, int length) { |
| 85 | + |
| 86 | + return StringUtils.truncate(text, length); |
| 87 | + } |
| 88 | + |
| 89 | + public static String usingSplitter(String text, int length) { |
| 90 | + if (length < 0) { |
| 91 | + throw new IllegalArgumentException("length cannot be negative"); |
| 92 | + } |
| 93 | + |
| 94 | + if (text == null) { |
| 95 | + return EMPTY; |
| 96 | + } |
| 97 | + |
| 98 | + Iterable<String> parts = Splitter.fixedLength(length) |
| 99 | + .split(text); |
| 100 | + |
| 101 | + return parts.iterator() |
| 102 | + .next(); |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments