File tree Expand file tree Collapse file tree
core-java-modules/core-java-regex/src/test/java/com/baeldung/replaceall Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .replaceall ;
2+
3+ import org .junit .Test ;
4+
5+ import static org .junit .Assert .assertEquals ;
6+
7+ public class ReplaceAllWithWhitespaceRegexUnitTest {
8+ private static final String INPUT_STR = "Text With Whitespaces! " ;
9+
10+ @ Test
11+ public void givenString_whenReplaceBySingleCharClass_thenGetExpect () {
12+ String expected = "Text___With_____Whitespaces!___" ;
13+ String result = INPUT_STR .replaceAll ("\\ s" , "_" );
14+ assertEquals (expected , result );
15+ }
16+
17+ @ Test
18+ public void givenString_whenReplaceBySingleCharClassWithPlus_thenGetExpect () {
19+ String expected = "Text_With_Whitespaces!_" ;
20+ String result = INPUT_STR .replaceAll ("\\ s+" , "_" );
21+ assertEquals (expected , result );
22+ }
23+
24+ @ Test
25+ public void givenString_whenRemoveByWhitespace_thenGetSameResult () {
26+ String expected = "TextWithWhitespaces!" ;
27+ String result1 = INPUT_STR .replaceAll ("\\ s" , "" );
28+ String result2 = INPUT_STR .replaceAll ("\\ s+" , "" );
29+ assertEquals (expected , result1 );
30+ assertEquals (result1 , result2 );
31+ }
32+ }
You can’t perform that action at this time.
0 commit comments