From 8c54cce5d14b20a420348c8fbeb3f0e99a7fdfae Mon Sep 17 00:00:00 2001 From: ACHRAF TAITAI <43656331+achraftt@users.noreply.github.com> Date: Sat, 2 Sep 2023 16:35:17 +0200 Subject: [PATCH] BAEL-6847: Replacing Single Quotes in Java String (#14665) --- .../replace/ReplaceStringUnitTest.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 core-java-modules/core-java-string-operations-5/src/test/java/com/baeldung/replace/ReplaceStringUnitTest.java diff --git a/core-java-modules/core-java-string-operations-5/src/test/java/com/baeldung/replace/ReplaceStringUnitTest.java b/core-java-modules/core-java-string-operations-5/src/test/java/com/baeldung/replace/ReplaceStringUnitTest.java new file mode 100644 index 0000000000..081ffabab1 --- /dev/null +++ b/core-java-modules/core-java-string-operations-5/src/test/java/com/baeldung/replace/ReplaceStringUnitTest.java @@ -0,0 +1,22 @@ +package com.baeldung.replace; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +public class ReplaceStringUnitTest { + private final String ORIGINAL_STRING = "This is 'Baeldung' tutorial."; + private final String EXPECTED_STRING = "This is \\'Baeldung\\' tutorial."; + + @Test + public void givenString_thenReplaceUsinReplaceAllMethod() { + String modifiedString = ORIGINAL_STRING.replaceAll("'", "\\\\'"); + assertEquals(EXPECTED_STRING, modifiedString); + } + + @Test + public void givenString_thenReplaceUsinReplaceMethod() { + String modifiedString = ORIGINAL_STRING.replace("'", "\\'"); + assertEquals(EXPECTED_STRING, modifiedString); + } +} \ No newline at end of file