Add the functionality and the unit testing for LTrim and RTrim

This commit is contained in:
Cavero Barca 2020-02-28 20:59:38 +01:00
parent 76f8c05fac
commit 647e4ffe0e
3 changed files with 216 additions and 0 deletions

View File

@ -9,4 +9,5 @@ This module contains articles about string operations.
- [String toLowerCase and toUpperCase Methods in Java](https://www.baeldung.com/java-string-convert-case)
- [Java String equalsIgnoreCase()](https://www.baeldung.com/java-string-equalsignorecase)
- [Case-Insensitive String Matching in Java](https://www.baeldung.com/java-case-insensitive-string-matching)
- [L-Trim and R-Trim in Java](https://www.baeldung.com/l-trim-and-r-trim-in-java)
- More articles: [[<-- prev]](../core-java-string-operations)

View File

@ -0,0 +1,138 @@
package com.baeldung.trim;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import com.google.common.base.CharMatcher;
/**
* Based on https://github.com/tedyoung/indexof-contains-benchmark
*/
@Fork(5)
@State(Scope.Benchmark)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class LTrimRTrim {
private String src;
private static String ltrimResult;
private static String rtrimResult;
private static Pattern LTRIM = Pattern.compile("^\\s+");
private static Pattern RTRIM = Pattern.compile("\\s+$");
public static void main(String[] args) throws Exception {
org.openjdk.jmh.Main.main(args);
}
@Setup
public void setup() {
src = " White spaces left and right ";
ltrimResult = "White spaces left and right ";
rtrimResult = " White spaces left and right";
}
public static String whileLtrim(String s) {
int i = 0;
while (i < s.length() && Character.isWhitespace(s.charAt(i))) {
i++;
}
return s.substring(i);
}
public static String whileRtrim(String s) {
int i = s.length()-1;
while (i >= 0 && Character.isWhitespace(s.charAt(i))) {
i--;
}
return s.substring(0,i+1);
}
private static boolean checkStrings(String ltrim, String rtrim) {
boolean result = false;
if (ltrimResult.equalsIgnoreCase(ltrim) && rtrimResult.equalsIgnoreCase(rtrim))
result = true;
return result;
}
// Going through the String detecting Whitespaces
@Benchmark
public boolean whileCharacters() {
String ltrim = whileLtrim(src);
String rtrim = whileRtrim(src);
return checkStrings(ltrim, rtrim);
}
// replaceAll() and Regular Expressions
@Benchmark
public boolean replaceAllRegularExpression() {
String ltrim = src.replaceAll("^\\s+", "");
String rtrim = src.replaceAll("\\s+$", "");
return checkStrings(ltrim, rtrim);
}
public static String patternLtrim(String s) {
return LTRIM.matcher(s).replaceAll("");
}
public static String patternRtrim(String s) {
return RTRIM.matcher(s).replaceAll("");
}
// Pattern matches() with replaceAll
@Benchmark
public boolean patternMatchesLTtrimRTrim() {
String ltrim = patternLtrim(src);
String rtrim = patternRtrim(src);
return checkStrings(ltrim, rtrim);
}
public static String guavaLtrim(String s) {
return CharMatcher.whitespace().trimLeadingFrom(s);
}
public static String guavaRtrim(String s) {
return CharMatcher.whitespace().trimTrailingFrom(s);
}
// Guava CharMatcher trimLeadingFrom / trimTrailingFrom
@Benchmark
public boolean guavaCharMatcher() {
String ltrim = guavaLtrim(src);
String rtrim = guavaRtrim(src);
return checkStrings(ltrim, rtrim);
}
public static String stringUtilsRTrim(String str) {
return org.apache.commons.lang3.StringUtils.stripEnd(str, " ");
}
public static String stringUtilsLTrim(String str) {
return org.apache.commons.lang3.StringUtils.stripStart(str, " ");
}
// Apache Commons StringUtils containsIgnoreCase
@Benchmark
public boolean apacheCommonsStringUtils() {
String ltrim = stringUtilsLTrim(src);
String rtrim = stringUtilsRTrim(src);
return checkStrings(ltrim, rtrim);
}
}

View File

@ -0,0 +1,77 @@
package com.baeldung.trim;
import org.apache.commons.lang3.StringUtils;
import org.junit.Assert;
import org.junit.Test;
import java.util.regex.Pattern;
/**
* BAEL-3755: LTrim and RTrim examples.
*/
public class LTrimRTrimUnitTest {
private String src = " White spaces left and right ";
private final static String ltrimResult = "White spaces left and right ";
private final static String rtrimResult = " White spaces left and right";
@Test
public void givenString_whenCallingWhileCharacters_thenReturnsTrue() {
String ltrim = LTrimRTrim.whileLtrim(src);
String rtrim = LTrimRTrim.whileRtrim(src);
// Compare the Strings obtained and the expected
Assert.assertTrue(ltrimResult.equalsIgnoreCase(ltrim));
Assert.assertTrue(rtrimResult.equalsIgnoreCase(rtrim));
}
@Test
public void givenString_whenCallingContainsWithReplaceAll_shouldReturnTrue() {
// Use replaceAll with Regular Expressions
String ltrim = src.replaceAll("^\\s+", "");
String rtrim = src.replaceAll("\\s+$", "");
// Compare the Strings obtained and the expected
Assert.assertTrue(ltrimResult.equalsIgnoreCase(ltrim));
Assert.assertTrue(rtrimResult.equalsIgnoreCase(rtrim));
}
@Test
public void givenString_whenCallingPaternCompileMatcherReplaceAll_thenReturnsTrue() {
// Use Pattern Compile Matcher and Find to avoid case insensitive issues
String ltrim = LTrimRTrim.patternLtrim(src);
String rtrim = LTrimRTrim.patternRtrim(src);
// Compare the Strings obtained and the expected
Assert.assertTrue(ltrimResult.equalsIgnoreCase(ltrim));
Assert.assertTrue(rtrimResult.equalsIgnoreCase(rtrim));
}
@Test
public void givenString_whenCallingGuavaCharMatcher_thenReturnsTrue() {
// Use StringUtils containsIgnoreCase to avoid case insensitive issues
String ltrim = LTrimRTrim.guavaLtrim(src);
String rtrim = LTrimRTrim.guavaRtrim(src);
// Compare the Strings obtained and the expected
Assert.assertTrue(ltrimResult.equalsIgnoreCase(ltrim));
Assert.assertTrue(rtrimResult.equalsIgnoreCase(rtrim));
}
@Test
public void givenString_whenCallingStringUtilsStripStartEnd_thenReturnsTrue() {
// Use StringUtils containsIgnoreCase to avoid case insensitive issues
String ltrim = LTrimRTrim.stringUtilsLTrim(src);
String rtrim = LTrimRTrim.stringUtilsRTrim(src);
// Compare the Strings obtained and the expected
Assert.assertTrue(ltrimResult.equalsIgnoreCase(ltrim));
Assert.assertTrue(rtrimResult.equalsIgnoreCase(rtrim));
}
}