BAEL-6847: Replacing Single Quotes in Java String (#14665)

This commit is contained in:
ACHRAF TAITAI 2023-09-02 16:35:17 +02:00 committed by GitHub
parent 8c3d581fa9
commit 8c54cce5d1
1 changed files with 22 additions and 0 deletions

View File

@ -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);
}
}