-1

I found some help from this question: How can I trim beginning and ending double quotes from a string?

CharMatcher.is('\"').trimFrom(mystring);

I am looking for solution without using regex.

However the problem I am facing with this approach is that it trims all occurrence of quotes. For example if mystring ends with two trailing quotes then it removes both. I want to remove only single character and not both.

Community
  • 1
  • 1
vatsal mevada
  • 5,148
  • 7
  • 39
  • 68
  • 3
    possible duplicate of [How can I trim beginning and ending double quotes from a string?](http://stackoverflow.com/questions/2608665/how-can-i-trim-beginning-and-ending-double-quotes-from-a-string) – Avinash Raj Aug 18 '15 at 14:41
  • 2
    did you see the accepted answer? – Avinash Raj Aug 18 '15 at 14:41
  • Sorry for the incomplete question. Of course I would have read accepted answer before scrolling down. However I would like to avoid regex for such trivial operation. Hence was looking for some clean alternative like I found in the other answer. – vatsal mevada Aug 18 '15 at 15:20

2 Answers2

0

Check out this code:

    String x = "\"  blabla \"\"";
    int firstOccurence = x.indexOf('"');
    int secondOccurence = x.lastIndexOf('"');
    if(firstOccurence != secondOccurence){
        x = x.substring(firstOccurence + 1, secondOccurence);
    }
    System.out.println(x);

it returns:

  blabla "
Tawcharowsky
  • 615
  • 4
  • 18
0

try this code for removing the first and last " :

public String method(String str) {
//Remove the last "
if (str.length() > 0 && str.charAt(str.length()-1)=='\"') {
  str = str.substring(0, str.length()-1);
}
//Remove the first "
if (str.length() > 0 && str.charAt(0)=='\"') {
  str = str.substring(0, str.length()-1);
}
return str;}
devlopp
  • 3
  • 1
  • 7