Remove Beginning and Ending Double Quotes from a String
https://www.baeldung.com/java-remove-start-end-double-quote#:~:text=To%20remove%20double%20quotes%20just,be%20replaced%20by%20empty%20strings.
1. Overview
In this article, we're going to study different approaches for removing beginning and ending double quotes from a String in 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
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.
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 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:
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.
3. Using the replaceAll Method
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:
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
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.
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.
Finally, we have escaped double quotes followed by a dollar symbol ($) for matching double quotes at the end of the String.
4. Using Guava
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:
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:
<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.
5. Conclusion
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.
As always, the code presented in this article is available over on GitHub.