Skip to content

Commit 5145b90

Browse files
SeunMattmaibin
authored andcommitted
added example code for BAEL-2366 (eugenp#5730)
1 parent ba98695 commit 5145b90

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.baeldung.string;
2+
3+
4+
import org.apache.commons.lang3.StringUtils;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertFalse;
8+
import static org.junit.Assert.assertTrue;
9+
10+
public class StringReplaceAndRemoveUnitTest {
11+
12+
13+
@Test
14+
public void givenTestStrings_whenReplace_thenProcessedString() {
15+
16+
String master = "Hello World Baeldung!";
17+
String target = "Baeldung";
18+
String replacement = "Java";
19+
String processed = master.replace(target, replacement);
20+
assertTrue(processed.contains(replacement));
21+
assertFalse(processed.contains(target));
22+
23+
}
24+
25+
@Test
26+
public void givenTestStrings_whenReplaceAll_thenProcessedString() {
27+
28+
String master2 = "Welcome to Baeldung, Hello World Baeldung";
29+
String regexTarget= "(Baeldung)$";
30+
String replacement = "Java";
31+
String processed2 = master2.replaceAll(regexTarget, replacement);
32+
assertTrue(processed2.endsWith("Java"));
33+
34+
}
35+
36+
@Test
37+
public void givenTestStrings_whenStringBuilderMethods_thenProcessedString() {
38+
39+
String master = "Hello World Baeldung!";
40+
String target = "Baeldung";
41+
String replacement = "Java";
42+
43+
int startIndex = master.indexOf(target);
44+
int stopIndex = startIndex + target.length();
45+
46+
StringBuilder builder = new StringBuilder(master);
47+
48+
49+
builder.delete(startIndex, stopIndex);
50+
assertFalse(builder.toString().contains(target));
51+
52+
53+
builder.replace(startIndex, stopIndex, replacement);
54+
assertTrue(builder.toString().contains(replacement));
55+
56+
57+
}
58+
59+
60+
@Test
61+
public void givenTestStrings_whenStringUtilsMethods_thenProcessedStrings() {
62+
63+
String master = "Hello World Baeldung!";
64+
String target = "Baeldung";
65+
String replacement = "Java";
66+
67+
String processed = StringUtils.replace(master, target, replacement);
68+
assertTrue(processed.contains(replacement));
69+
70+
String master2 = "Hello World Baeldung!";
71+
String target2 = "baeldung";
72+
String processed2 = StringUtils.replaceIgnoreCase(master2, target2, replacement);
73+
assertFalse(processed2.contains(target));
74+
75+
}
76+
77+
78+
79+
80+
81+
82+
83+
}

0 commit comments

Comments
 (0)