Merge pull request #8897 from sk1418/master
[BAEL-3914] java regex: \\s vs \\s+
This commit is contained in:
commit
99c2e2ea2a
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue