1. Overview
1.概述
In Java, single quotes are used to define char literals, and double quotes define String literals. We can also use single quotes in String literals. In this tutorial, we’ll learn how to replace single quotes in Java String.
在 Java 中,单引号用于定义字符字面量,双引号用于定义 String 字面量。我们也可以在字符串字面量中使用单引号。在本教程中,我们将学习 如何在 Java String 中替换单引号。
2. How to Escape Special Characters in Java?
2. 如何在 Java 中转义特殊字符?
A String is a sequence of characters. These characters can be letters, numbers, punctuation marks, etc. When creating a String, it must be enclosed in double quotes, but what should we do if we need to create a String that must contain single quotes itself? Well, Java will misunderstand the String and throw an error because a single quote is interpreted as a special character.
字符串是一串字符。这些字符可以是字母、数字、标点符号等。创建 String 时,必须用双引号括起来,但如果我们需要创建一个本身必须包含单引号的 String 时,该怎么办呢?那么,Java 将误解 String 并抛出错误,因为单引号被解释为特殊字符。
To solve this problem, we can simply use one of the escaping characters, such as the special backslash \ character that turns special characters into String characters.
要解决这个问题,我们可以简单地使用 转义字符,例如将特殊字符转换为字符串字符的特殊反斜杠字符。
For instance, let’s consider the case where we want to replace the single quotes in a String with \’.
例如,让我们考虑这样一种情况:我们想用\’替换 String 中的单引号。
3. Using String.replace()
3. 使用 String.replace()
Let’s use the String.replace(CharSequence target, CharSequence replacement) method to do the String replacement. This method replaces all occurrences of the target with replacement.
让我们使用 String.replace(CharSequence target, CharSequence replacement) 方法来进行 String 替换。该方法将 target 的所有出现次数替换为 replacement。
Let’s see how to use the String.replace() method to replace single quotes in Java String:
让我们看看如何使用 String.replace() 方法替换 Java String 中的单引号:
String ORIGINAL_STRING = "This is 'Baeldung' tutorial.";
String EXPECTED_STRING = "This is \\'Baeldung\\' tutorial.";
@Test
public void givenString_thenReplaceUsinReplaceMethod() {
String modifiedString = ORIGINAL_STRING.replace("'", "\\'");
assertEquals(EXPECTED_STRING, modifiedString);
}
In the above example, we used the value \\’ as a replacement parameter in the String.replace() method. The first backslash escapes the second one, and together with the single quote, it forms the escaped sequence \’. In this simple way, we replaced the single quotes in the desired String with \’.
在上例中,我们将值 \\’ 用作 String.replace() 方法中的替换参数。第一个反斜杠转义了第二个反斜杠,它与单引号一起构成了转义序列 \’。通过这种简单的方法,我们用 \’ 替换了所需字符串中的单引号。
4. Using String.replaceAll()
4. 使用 String.replaceAll()
The String.replaceAll(String regex, String replacement) method is similar to the String.replace(CharSequence target, CharSequence replacement). The main difference between them is in how they handle the substrings to replace. Furthermore, replace() works with plain text and replaces literal occurrences, while replaceAll() works with regular expressions.
String.replaceAll(String regex, String replacement) 方法类似于 String.replace(CharSequence target, CharSequence replacement)。它们之间的主要区别在于如何处理要替换的子串。此外,replace() 适用于纯文本并替换字面出现的内容,而 replaceAll() 适用于正则表达式。
For demonstration purposes, we’ll use the same example as the previous section:
为了便于演示,我们将使用与上一节相同的示例:
@Test
public void givenString_thenReplaceUsinReplaceAllMethod() {
String modifiedString = ORIGINAL_STRING.replaceAll("'", "\\\\'");
assertEquals(EXPECTED_STRING, modifiedString);
}
In the above example, we used the value \\\\’ as a replacement parameter in the String.replaceAll() method. The first two backslashes are used to correctly escape the backslash character in the regular expression, and then \\’ represents the escaped single quote character \’.
在上面的示例中,我们使用 \\\\’ 作为 String.replaceAll() 方法中的替换参数。前两个反斜杠用于正确转义正则表达式中的反斜杠字符,然后 \\’ 表示转义后的单引号字符 \’。
Remember that Strings in Java are immutable, meaning that the replace() or replaceAll() methods don’t modify the original String but instead return a new modified String. So, make sure to assign the result of the method call to a new variable.
请记住,Java 中的字符串是不可变的,这意味着replace()或 replaceAll() 方法不会修改原始字符串,而是返回一个修改后的新字符串。因此,请确保将方法调用的结果赋值给一个新变量。
5. Conclusion
5.结论
In this short article, we learned how to replace single quotes in Java String. The source code is available over on GitHub.