BAEL-5693 rm whitespace from a string (#12515)

This commit is contained in:
Kai Yuan 2022-07-22 04:58:46 +02:00 committed by GitHub
parent 8d894df26a
commit 6b8313682e
1 changed files with 35 additions and 0 deletions

View File

@ -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 !");
}
}