Skip to content

Commit d46370d

Browse files
committed
BAEL-5550: Trim a string based on the string length
1 parent 197cd50 commit d46370d

2 files changed

Lines changed: 158 additions & 0 deletions

File tree

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.truncate;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class TruncateStringUnitTest {
8+
9+
private static final String TEXT = "Welcome to baeldung.com";
10+
11+
@Test
12+
public void givenStringAndLength_whenUsingSubstringMethod_thenTruncate() {
13+
14+
assertEquals(TruncateString.usingSubstringMethod(TEXT, 10), "Welcome to");
15+
}
16+
17+
@Test
18+
public void givenStringAndLength_whenUsingSplitMethod_thenTruncate() {
19+
20+
assertEquals(TruncateString.usingSplitMethod(TEXT, 13), "Welcome to ba");
21+
}
22+
23+
@Test
24+
public void givenStringAndLength_whenUsingPattern_thenTruncate() {
25+
26+
assertEquals(TruncateString.usingPattern(TEXT, 19), "Welcome to baeldung");
27+
}
28+
29+
@Test
30+
public void givenStringAndLength_whenUsingCodePointsMethod_thenTruncate() {
31+
32+
assertEquals(TruncateString.usingCodePointsMethod(TEXT, 6), "Welcom");
33+
}
34+
35+
@Test
36+
public void givenStringAndLength_whenUsingLeftMethod_thenTruncate() {
37+
38+
assertEquals(TruncateString.usingLeftMethod(TEXT, 15), "Welcome to bael");
39+
}
40+
41+
@Test
42+
public void givenStringAndLength_whenUsingTruncateMethod_thenTruncate() {
43+
44+
assertEquals(TruncateString.usingTruncateMethod(TEXT, 20), "Welcome to baeldung.");
45+
}
46+
47+
@Test
48+
public void givenStringAndLength_whenUsingSplitter_thenTruncate() {
49+
50+
assertEquals(TruncateString.usingSplitter(TEXT, 3), "Wel");
51+
}
52+
53+
}

0 commit comments

Comments
 (0)