Merge pull request #15921 from sk1418/rm-trailing-space

[rm-trailing-space] rm trailing whithspace
This commit is contained in:
Liam Williams 2024-02-27 21:18:16 +00:00 committed by GitHub
commit 72953d3c13
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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);
}
}