• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 665
  • Last Modified:

parsing strings strtok() and delimiters

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!
1 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

>> 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 '#'
Bob LambersonSoftware EngineerCommented:
Using a pipe symbol "|"is usually safer than either "\" or "#", since you hardly ever find one in a string.

how about a nonprintable character - '\xFF'?
bjw5d6Author Commented:
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
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.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

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