[rm-trailing-space] rm trailing whithspace

This commit is contained in:
Kai.Yuan 2024-02-20 07:27:46 +08:00
parent bd36eddeca
commit 501c511e74
2 changed files with 48 additions and 1 deletions

View File

@ -12,6 +12,14 @@
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${apache.commons.lang3.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
@ -29,6 +37,7 @@
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<apache.commons.lang3.version>3.14.0</apache.commons.lang3.version>
</properties>
</project>
</project>

View File

@ -0,0 +1,38 @@
package com.baeldung.removetrailingspaces;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
public class RemoveTrailingSpaceOrWhitespaceUnitTest {
private final static String INPUT = " a b c d e \t ";
@Test
void whenUsingTrim_thenBothLeadingAndTrailingWhitespaceAreRemoved() {
String result = INPUT.trim();
assertEquals("a b c d e", result);
}
@Test
void whenUsingReplaceAll_thenGetExpectedResult() {
String result1 = INPUT.replaceAll(" +$", "");
assertEquals(" a b c d e \t", result1);
String result2 = INPUT.replaceAll("\\s+$", "");
assertEquals(" a b c d e", result2);
}
@Test
void whenUsingStripTrailing_thenAllTrailingWhitespaceRemoved() {
String result = INPUT.stripTrailing();
assertEquals(" a b c d e", result);
}
@Test
void whenUsingStringUtilsStripEnd_thenTrailingSpaceRemoved() {
String result = StringUtils.stripEnd(INPUT, " ");
assertEquals(" a b c d e \t", result);
}
}