1. Overview
1.概述
In this article, we’re going to study different approaches for removing beginning and ending double quotes from a String in Java.
在这篇文章中,我们将研究在Java中去除字符串的开头和结尾双引号的不同方法。
What we’ll explore here can be useful for processing text extracted from files or received from other sources.
我们将在这里探讨的内容对处理从文件中提取的或从其他来源收到的文本很有用。
2. Simple Approach: the substring Method
2.简单的方法:子串方法
Let’s first start with a simple approach by using the substring method. This method can be called on a String object to return a specific sub-string.
首先让我们从一个简单的方法开始,使用substring方法。这个方法可以在一个String对象上调用,以返回一个特定的子字符串。
The method requires two parameters:
该方法需要两个参数。
- beginIndex — the index of the character where the sub-string should begin
- endIndex — the index after where the sub-string should end
Thus, considering that our input String is enclosed in double-quotes, we can use the substring method:
因此,考虑到我们的输入String是用双引号括起来的,我们可以使用substring方法。
String input = "\"text wrapped in double quotes\"";
String result = input.substring(1, input.length() - 1);
System.out.println("Input: " + input);
System.out.println("Result: " + result);
By executing the code above, we have the following output:
通过执行上面的代码,我们有以下输出。
Input: "text wrapped in double quotes"
Result: text wrapped in double quotes
When we are unsure whether the String will be enclosed in double-quotes or not, we should check it before running the substring method:
当我们不确定String是否会被包含在双引号中时,我们应该在运行substring方法之前进行检查。
if (input != null && input.length() >= 2
&& input.charAt(0) == '\"' && input.charAt(input.length() - 1) == '\"') {
result = input.substring(1, input.length() - 1);
}
In the example above, we check that the String has at least two characters and that it starts and ends with double quotes.
在上面的例子中,我们检查String是否至少有两个字符,以及是否以双引号开始和结束。
3. Using the replaceAll Method
3.使用replaceAll方法
In addition to the substring method, we can also use the replaceAll method. This method replaces all parts of the String that match a given regular expression. Using replaceAll, we can remove all occurrences of double quotes by replacing them with empty strings:
除了substring方法之外,我们还可以使用replaceAll方法。该方法替换了String中与给定的正则表达式相匹配的所有部分。使用replaceAll,我们可以将所有出现的双引号替换为空字符串,从而删除它们。
String result = input.replaceAll("\"", "");
On one hand, this approach has the advantage of removing all occurrences of double quotes, even if the string has multiple lines. On the other hand, with this approach, we’re unable to remove only the double quotes at the beginning and end of the String.
一方面,这种方法的优点是可以删除所有出现的双引号,即使字符串有多行。另一方面,使用这种方法,我们无法只删除String.开头和结尾的双引号。
To remove double quotes just from the beginning and end of the String, we can use a more specific regular expression:
要想只从字符串的开头和结尾去除双引号,我们可以使用一个更具体的正则表达式。
String result = input.replaceAll("^\"|\"$", "");
After executing this example, occurrences of double quotes at the beginning or at end of the String will be replaced by empty strings.
执行这个例子后,在String的开头或结尾出现的双引号将被替换成空字符串。
To understand this approach, let’s break down our regular expression.
为了理解这种方法,让我们分解一下我们的正则表达式。
First, we have a caret symbol (^) followed by escaped double quotes (\”) for matching double quotes at the beginning of the String. Then, there is a pipe symbol (|) to indicate a matching alternative — similar to the OR logical operator.
首先,我们有一个圆点符号(^),后面是转义的双引号(\”),用于匹配String开头的双引号。然后,有一个管道符号(|)来表示匹配的备选方案–类似于OR逻辑运算符。
Finally, we have escaped double quotes followed by a dollar symbol ($) for matching double quotes at the end of the String.
最后,我们在String的结尾处用转义的双引号和美元符号($)来匹配双引号。
4. Using Guava
4.使用番石榴
Another possible approach for removing double quotes from the beginning and end of a String is to use the CharMatcher class from the Guava library:
另一种去除字符串开头和结尾的双引号的可能方法是使用Guava库中的CharMatcher类。
String result = CharMatcher.is('\"').trimFrom(input);
This approach is simpler to understand and removes only the beginning and end quotes from the String. However, for this approach to work, we need to add the guava library to our project:
这种方法更简单易懂,它只删除了String中的开头和结尾的引号。然而,为了使这种方法发挥作用,我们需要在我们的项目中添加guava库。
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava-version}</version>
</dependency>
In this case, we need to set the ${guava-version} property to the version we want to use.
在这种情况下,我们需要将${guava-version}属性设置为我们要使用的版本。
5. Conclusion
5.总结
In this article, we explored different alternatives for removing double quotes at the beginning and at the end of a String. We can apply any of these approaches in practice. Each of them has advantages and disadvantages.
在这篇文章中,我们探讨了不同的替代方法,用于移除字符串的开头和结尾的双引号。我们可以在实践中应用这些方法中的任何一种。它们中的每一个都有优点和缺点。
For example, when using the Guava library, we have a simple and elegant solution. However, in case Guava wasn’t included in our project, this solution would require the addition of a new dependency.
例如,当使用Guava库时,我们有一个简单而优雅的解决方案。然而,如果Guava没有包含在我们的项目中,这个解决方案就需要增加一个新的依赖关系。
As always, the code presented in this article is available over on GitHub.
一如既往,本文介绍的代码可在GitHub上获得。