BAEL-5693 rm whitespace from a string (#12515)
This commit is contained in:
parent
8d894df26a
commit
6b8313682e
|
@ -0,0 +1,35 @@
|
||||||
|
package com.baeldung.removewhitespace;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
class RemoveWhitespaceUnitTest {
|
||||||
|
private final String myString = " I am a wonderful String ! ";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenStringWithWhitespace_whenRemoveAllWhitespace_shouldGetExpectedResult() {
|
||||||
|
String result = myString.replaceAll("\\s", "");
|
||||||
|
assertThat(result).isEqualTo("IamawonderfulString!");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenStringWithWhitespace_whenRemoveAllWhitespaceUsingStringUtils_shouldGetExpectedResult() {
|
||||||
|
String result = StringUtils.deleteWhitespace(myString);
|
||||||
|
assertThat(result).isEqualTo("IamawonderfulString!");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenStringWithWhitespace_whenReplaceConsecutiveSpacesWithSingleSpace_shouldGetExpectedResult() {
|
||||||
|
String result = myString.replaceAll("\\s+", " ");
|
||||||
|
assertThat(result).isEqualTo(" I am a wonderful String ! ");
|
||||||
|
assertThat(result.trim()).isEqualTo("I am a wonderful String !");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenStringWithWhitespace_whenNormalizeWithApacheCommons_shouldGetExpectedResult() {
|
||||||
|
String result = StringUtils.normalizeSpace(myString);
|
||||||
|
assertThat(result).isEqualTo("I am a wonderful String !");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue