File tree Expand file tree Collapse file tree
core-java-modules/core-java-string-operations-9/src/test/java/com/baeldung/removeallcharsbeforechar Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .removeallcharsbeforechar ;
2+
3+ import org .junit .Test ;
4+
5+ import static org .junit .Assert .assertEquals ;
6+
7+ public class RemoveAllCharsBeforeSpecificOneUnitTest {
8+ String inputString = "Hello World!" ;
9+ char targetCharacter = 'W' ;
10+
11+ @ Test
12+ public void givenString_whenUsingSubstring_thenCharactersRemoved () {
13+ String result = inputString .substring (inputString .indexOf (targetCharacter ));
14+ assertEquals ("World!" , result );
15+ }
16+
17+ @ Test
18+ public void givenString_whenUsingRegex_thenCharactersRemoved () {
19+ String result = (targetCharacter ) + inputString .replaceAll (".*" + targetCharacter , "" );
20+ assertEquals ("World!" , result );
21+ }
22+
23+ @ Test
24+ public void givenString_whenUsingStringBuilder_thenCharactersRemoved () {
25+ StringBuilder sb = new StringBuilder (inputString );
26+ int index = sb .indexOf (String .valueOf (targetCharacter ));
27+ if (index != -1 ) {
28+ sb .delete (0 , index );
29+ }
30+ assertEquals ("World!" , sb .toString ());
31+ }
32+ }
You can’t perform that action at this time.
0 commit comments