This commit is related to BAEL-7858 (#16507)

This commit aims to add a test class "RemoveAllCharsBeforeSpecificOneUnitTest".
This commit is contained in:
Mo Helmy 2024-04-28 00:02:06 +03:00 committed by GitHub
parent 14c41ff72a
commit eca1d4920a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 32 additions and 0 deletions

View File

@ -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());
}
}