Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


parsing strings strtok() and delimiters

Posted on 2004-10-22
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

Sicos earned 750 total points
ID: 12385575


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


Expert Comment

ID: 12386739
>> 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

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


Expert Comment

ID: 12395442
how about a nonprintable character - '\xFF'?

Author Comment

ID: 12396364
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
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…

636 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