Avatar of jbaird123
jbaird123

asked on 

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?

Java

Avatar of undefined
Last Comment
jbaird123
Avatar of for_yan
for_yan
Flag of United States of America image


if(s.startsWith("\"\""))s = s.substring(1,s.length()-1);
Avatar of for_yan
for_yan
Flag of United States of America image

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


Avatar of jbaird123
jbaird123

ASKER

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.
Avatar of for_yan
for_yan
Flag of United States of America image

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

ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of jbaird123
jbaird123

ASKER

Thanks!
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo