examples for BAEL-5153 - remove double quotes (#11344)
This commit is contained in:
parent
6d1cdbe9d7
commit
979db86a51
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue