This commit is related to BAEL-7858 (#16507)
This commit aims to add a test class "RemoveAllCharsBeforeSpecificOneUnitTest".
This commit is contained in:
parent
14c41ff72a
commit
eca1d4920a
@ -0,0 +1,32 @@
|
|||||||
|
package com.baeldung.removeallcharsbeforechar;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class RemoveAllCharsBeforeSpecificOneUnitTest {
|
||||||
|
String inputString = "Hello World!";
|
||||||
|
char targetCharacter = 'W';
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenString_whenUsingSubstring_thenCharactersRemoved() {
|
||||||
|
String result = inputString.substring(inputString.indexOf(targetCharacter));
|
||||||
|
assertEquals("World!", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenString_whenUsingRegex_thenCharactersRemoved() {
|
||||||
|
String result = (targetCharacter) + inputString.replaceAll(".*" + targetCharacter, "");
|
||||||
|
assertEquals("World!", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenString_whenUsingStringBuilder_thenCharactersRemoved() {
|
||||||
|
StringBuilder sb = new StringBuilder(inputString);
|
||||||
|
int index = sb.indexOf(String.valueOf(targetCharacter));
|
||||||
|
if (index != -1) {
|
||||||
|
sb.delete(0, index);
|
||||||
|
}
|
||||||
|
assertEquals("World!", sb.toString());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user