examples for BAEL-5153 - remove double quotes (#11344)

This commit is contained in:
Willian Nalepa Oizumi 2021-10-17 03:14:31 -03:00 committed by GitHub
parent 6d1cdbe9d7
commit 979db86a51
2 changed files with 79 additions and 0 deletions

View File

@ -0,0 +1,37 @@
package com.baeldung.doublequotesremoval;
import com.google.common.base.CharMatcher;
public class DoubleQuotesRemovalUtils {
public static String removeWithSubString(String input) {
if (input != null && input.length() >= 2 && input.charAt(0) == '\"'
&& input.charAt(input.length() - 1) == '\"') {
return input.substring(1, input.length() - 1);
}
return input;
}
public static String removeWithReplaceAllSimple(String input) {
if (input == null || input.isEmpty())
return input;
return input.replaceAll("\"", "");
}
public static String removeWithReplaceAllAdvanced(String input) {
if (input == null || input.isEmpty())
return input;
return input.replaceAll("^\"|\"$", "");
}
public static String removeWithGuava(String input) {
if (input == null || input.isEmpty())
return input;
return CharMatcher.is('\"').trimFrom(input);
}
}

View File

@ -0,0 +1,42 @@
package com.baeldung.doublequotesremoval;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.jupiter.api.Test;
public class DoubleQuotesRemovalUtilsUnitTest {
@Test
public void given_EmptyString_ShouldReturn_EmptyString() {
String input = "";
assertTrue(DoubleQuotesRemovalUtils.removeWithSubString(input).isEmpty());
assertTrue(DoubleQuotesRemovalUtils.removeWithReplaceAllSimple(input).isEmpty());
assertTrue(DoubleQuotesRemovalUtils.removeWithReplaceAllAdvanced(input).isEmpty());
assertTrue(DoubleQuotesRemovalUtils.removeWithGuava(input).isEmpty());
}
@Test
public void given_DoubleQuotesOnly_ShouldReturn_EmptyString() {
String input = "\"\"";
assertTrue(DoubleQuotesRemovalUtils.removeWithSubString(input).isEmpty());
assertTrue(DoubleQuotesRemovalUtils.removeWithReplaceAllSimple(input).isEmpty());
assertTrue(DoubleQuotesRemovalUtils.removeWithReplaceAllAdvanced(input).isEmpty());
assertTrue(DoubleQuotesRemovalUtils.removeWithGuava(input).isEmpty());
}
@Test
public void given_TextWithDoubleQuotes_ShouldReturn_TextOnly() {
String input = "\"Example of text for this test suit\"";
String expectedResult = "Example of text for this test suit";
assertEquals(expectedResult, DoubleQuotesRemovalUtils.removeWithSubString(input));
assertEquals(expectedResult, DoubleQuotesRemovalUtils.removeWithReplaceAllSimple(input));
assertEquals(expectedResult, DoubleQuotesRemovalUtils.removeWithReplaceAllAdvanced(input));
assertEquals(expectedResult, DoubleQuotesRemovalUtils.removeWithGuava(input));
}
}