parsing strings strtok() and delimiters

Posted on 2004-10-22
Last Modified: 2007-12-19
Alright, I'm trying to read in an input file that has 6 parts:

Media type, number, genre, title, artist, composer
cd,1,New Age,Glassworks,Philip Glass,Philip Glass

The problem is that sometimes the title, artist, and composer have commas inside
the name.  In this case the data line looks like:
cd,78,Classic,\Capriccio Italien,+++\,Eduardo Mata/Dallas,Tchaikovsky

so the comma cannot be used as a delimiter... How can I put an "if" statement on the
delimiter to say "if a backslash is read in, then the next delimiter will be a backslash"
"else, the next delimiter will be a comma."  

I can do this without using strtok(token, delimiter), but with the strtok I have no idea.
Please help.  Thanks!
Question by:bjw5d6
    LVL 4

    Accepted Solution



    Here's a procedure written by me that reformats the line, it replaces the "," and ",\" and "\," with a "#"
    After that you can easily use the split command to split the string.


    ReformatLine("cd,78,Classic,\Capriccio Italien,+++\,\Capriccio Italien,+++\,Eduardo Mata/Dallas,Tchaikovsky") gives

    cd#78#Classic#Capriccio Italien#+++#Capriccio Italien,+++#Eduardo Mata/Dallas#Tchaikovs

    Now you can use the Split command to split the string in parts

    Function ReformatLine(lsLine As String) As String

        Dim lsTemp, lsOutput As String
        Dim liI, liBegin, liEnd As Integer
        While (lsLine <> "")
            liBegin = InStr(1, lsLine, ",\")
            If (liBegin > 0) Then
                lsOutput = lsOutput & Mid(lsLine, 1, liBegin - 1)
                lsOutput = Replace(lsOutput, ",", "#")
                liEnd = InStr(liBegin + 2, lsLine, "\") + 1
                lsTemp = Mid(lsLine, liBegin, liEnd - liBegin)
                lsTemp = Replace(lsTemp, ",\", "#")
                lsTemp = Replace(lsTemp, "\", "")
                lsOutput = lsOutput & lsTemp
                lsLine = Mid(lsLine, liEnd, Len(lsLine) - liEnd)
                lsLine = Replace(lsLine, ",", "#")
                ReformatLine = lsOutput & lsLine
                lsLine = ""
            End If

    End Function

    LVL 9

    Expert Comment

    >> Here's a procedure written by me that reformats the line, it replaces the "," and ",\" and "\," with a "#"
    what if '#' is a part of the original string. i suggest using '\' instead of '#'
    LVL 12

    Expert Comment

    Using a pipe symbol "|"is usually safer than either "\" or "#", since you hardly ever find one in a string.

    LVL 9

    Expert Comment

    how about a nonprintable character - '\xFF'?

    Author Comment

    This is a good answer from Sicos, except what I meant was that  the
    \Capriccio Italien,+++\ was all supposed to be part of one token, so not
    split up like #Capriccio Italien#+++#... I just read the file in a longer way... but
    the answer was very much appreciated and is good knowledge to know:)  Thanks

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now