BAEL-6847: Replacing Single Quotes in Java String (#14665)
This commit is contained in:
parent
8c3d581fa9
commit
8c54cce5d1
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue