Merge pull request #8897 from sk1418/master

[BAEL-3914] java regex: \\s vs \\s+
This commit is contained in:
Eric Martin 2020-03-24 20:36:35 -05:00 committed by GitHub
commit 99c2e2ea2a
1 changed files with 32 additions and 0 deletions

View File

@ -0,0 +1,32 @@
package com.baeldung.replaceall;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class ReplaceAllWithWhitespaceRegexUnitTest {
private static final String INPUT_STR = "Text With Whitespaces! ";
@Test
public void givenString_whenReplaceBySingleCharClass_thenGetExpect() {
String expected = "Text___With_____Whitespaces!___";
String result = INPUT_STR.replaceAll("\\s", "_");
assertEquals(expected, result);
}
@Test
public void givenString_whenReplaceBySingleCharClassWithPlus_thenGetExpect() {
String expected = "Text_With_Whitespaces!_";
String result = INPUT_STR.replaceAll("\\s+", "_");
assertEquals(expected, result);
}
@Test
public void givenString_whenRemoveByWhitespace_thenGetSameResult() {
String expected = "TextWithWhitespaces!";
String result1 = INPUT_STR.replaceAll("\\s", "");
String result2 = INPUT_STR.replaceAll("\\s+", "");
assertEquals(expected, result1);
assertEquals(result1, result2);
}
}