Trim beginning and ending double quotes from a Java string

Trim beginning and ending double quotes from a Java string

1. Overview

In this article, we will learn to trim the beginning and ending double quotes from a Java string.

2. Trim beginning and ending double quotes from a Java string

We have several ways to trim the double quotes from the start and end of a Java string. Let’s see each one of them here.

2.1. Use String#replaceAll()

To trim the double quotes from the start and end of the string, use the replaceAll method of String.

In the below code sample, the replaceAll accepts the regex pattern "^\"|\"$" to match the beginning and ending double quotes of a String.

 public static void main(String args[]) {
      String x="\"This \" string starts and ends with double quotes.\"";
      System.out.println(x.replaceAll("^\"|\"$", "")); 
}
  1. ^ (carat) matches the specified string at the beginning of the string. Here, the ^\" regex matches the double quotes (") at the start of the string.
  2. $ (dollar) matches the specified string at the end of the string. Here, the \"$ regex matches the double quotes (") at the end of the string
  3. | (pipe) matches either the preceding regex or following it. Pipe in this ^\"|\"$ expression matches either ^\" (starting quotes) regex or \"$ (ending quotes).

Consider your string has only double quotes at the end, then this expression would still work. The below code contains a string with end quote and no start quote. However, this replaceAll works as expected by removing the end quote and returns the result.

public static void main(String args[]) {
   String x="This \" string ends with double quotes.\"";
   System.out.println(x.replaceAll("^\"|\"$", ""));
}

Besides, if your string has the double quotes only at the start and end, then you can use the below code:

x = x.replace("\"", "");

2.2. Use String#substring to trim beginning and ending double quotes of a Java string

You can use substring method to remove the beginning and ending double quotes from a string.

In the below code, we are using the substring method to remove the double quotes. This would work only if your string contains both the start and end double quotes (AND operation). However, you can customize based on your expectation.

jpublic static void main(String args[]) {
      String x="\"This \" string ends with double quotes.\"";
      if (x.length() >= 2 && x.startsWith("\"") && x.endsWith("\"")) {
        System.out.println(x.substring(1, x.length()-1));
      }
    }

2.3. Apache commons library

You can use the Apache commons library function StringUtils#strip to remove the double quotes from the start and end of the string. This requires adding a new apache commons library dependency in your project.

Now, let’s add the dependency to use the apache commons library.

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.11</version>
</dependency>

The below code would remove any beginning and ending double quotes from the string.

import org.apache.commons.lang3.StringUtils;
public class TrimDoubleQuotes {
    public static void main(String args[]) {
      String x="\"This \" string ends with double quotes.\"";
      System.out.println(StringUtils.strip(x, "\""));
    }
}

It behaves as an OR expression. For example, if the string has only beginning double quotes and no end quotes, it would still work.

import org.apache.commons.lang3.StringUtils;
public class TrimDoubleQuotes {
    public static void main(String args[]) {
      String x="This \" string ends with double quotes.\"";
      System.out.println(StringUtils.strip(x, "\""));
    }
}

2.4. Guava to trim beginning and ending double quotes of a Java string

Similarly, you can also use the CharMatcher method available in the Guava library. This is a simple solution. This requires guava dependency to be present in your project. You can check the latest Guava version and add to your project.

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>${guava-version}</version>
</dependency>

In the below code, the CharMatcher.is('\"') returns a character matcher that matches only the double quotes. Then, calling the trimFrom would match and remove the beginning and ending double quotes from the string x.

import com.google.common.base.CharMatcher;
public class TrimDoubleQuotes {
    public static void main(String args[]) {
      String x="\"This \" string ends with double quotes.\"";
      System.out.println(CharMatcher.is('\"').trimFrom(x));
    }
}

3. Conclusion

To sum up, we have seen various solutions to remove the leading and trailing double quotes from the String.

Based on your use case, you can select the right approach.

Leave a Comment