Java - how to trim double quotes

I have a java program which is processing a delimited text file.  Sometimes the data is surrounded by double quotes and I need to be able to strip the double quotes off.  However, sometimes a double quote actually is part of the data, and in that case, there are two double quotes, but I only want to strip off the last one.

Example:  If my data looks like this:

FirstName|LastName
"John"|"Smith"

I want my data values to be
FirstName = John
LastName = Smith

In this case, all double quotes were removed.

Here is a more difficult case:

FirstName|LastName|Alias
"John"|"Smith"|""The Bear""

I want the resulting data to be like this:

FirstName = John
LastName = Smith
Alias          = "The Bear"

In this case, I removed the outer set of double quotes but I did not trim ALL of the double quotes.

Is there any kind of trim function that I can use to trim only the first and last characters if they are double quotes?

jbaird123Asked:
Who is Participating?
 
for_yanCommented:

Added example without quotes at all:
      String squot=   "\"John\"";
      String squot1= "\"Smith\"";
        String squot2 = "\"\"The Bear\"\"";
         String squot3 = "Tom";


if(squot.startsWith("\""))squot = squot.substring(1,squot.length()-1);

        if(squot1.startsWith("\""))squot1 = squot1.substring(1,squot1.length()-1);

           if(squot2.startsWith("\""))squot2 = squot2.substring(1,squot2.length()-1);

            if(squot3.startsWith("\""))squot3 = squot3.substring(1,squot3.length()-1);


        System.out.println(squot);

          System.out.println(squot1);

          System.out.println(squot2);

         System.out.println(squot3);

Open in new window



John
Smith
"The Bear"
Tom

Open in new window

0
 
for_yanCommented:

if(s.startsWith("\"\""))s = s.substring(1,s.length()-1);
0
 
for_yanCommented:
No but you always want to strip waway one layer of quotes
so
if(s.startsWith("\""))s = s.substring(1,s.length()-1);


0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
jbaird123Author Commented:
for_yan:

I don't always want to strip away 1 layer of quotes.

Sometimes the data is like this:

FirstName|LastName|Alias
John|"Smith"|||The Bear""

In this case, I do not want to remove quotes from John since John is not enclosed in quotes.
0
 
for_yanCommented:
Still I'm not sure I undesratnd - if there is at least one layer of quotes you want to remove the outer one,
theh this should work:

      String squot=   "\"John\"";
      String squot1= "\"Smith\"";
        String squot2 = "\"\"The Bear\"\"";

if(squot.startsWith("\""))squot = squot.substring(1,squot.length()-1);

        if(squot1.startsWith("\""))squot1 = squot1.substring(1,squot1.length()-1);

           if(squot2.startsWith("\""))squot2 = squot2.substring(1,squot2.length()-1);


        System.out.println(squot);

          System.out.println(squot1);

          System.out.println(squot2);

Open in new window


John
Smith
"The Bear"

Open in new window

0
 
jbaird123Author Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.