Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 629
  • Last Modified:

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?

0
jbaird123
Asked:
jbaird123
  • 4
  • 2
1 Solution
 
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
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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
 
jbaird123Author Commented:
Thanks!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now