Java Program to remove the leading and trailing quotes from a string



Firstly, let us consider a string with quotes

String originalStr = "\"Demo Text\"";

Now, consider the following logic for the beginning quote.

if (originalStr.startsWith("\"")) {
   originalStr = originalStr.substring(1, originalStr.length());
}

Now, consider the following logic for the ending quote.

if (originalStr.endsWith("\"")) {
   originalStr = originalStr.substring(0, originalStr.length() - 1);
}

Example

 Live Demo

public class Demo {
   public static void main(String[] args) {
      String originalStr = "\"Demo Text\"";
      System.out.println("String with double quotes= "+originalStr);
      if (originalStr.startsWith("\"")) {
         originalStr = originalStr.substring(1, originalStr.length());
      }
      if (originalStr.endsWith("\"")) {
         originalStr = originalStr.substring(0, originalStr.length() - 1);
      }
      System.out.println("String after removing double quotes = "+originalStr);
   }
}

Output

String with double quotes= "Demo Text" String after removing double quotes = Demo Text

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know


Advertisements