Split space delimited string with text qualifiers.


Using VB.Net in Visual Studio 2003

I have a string (originates from a text file) that I'd need to split, into an array or seperate variables, it doesn't really matter.

The problem is, it's space delimited, and has double-quotes for text qualifiers round the text fileds.
"C" "01" "90000014" 1997 15/04/97 "D/D PAYMENT" "Mr John Smith" 47.58 11/04/97

The above example has 9 fields.  As you can see, the text fields have double-quotes round them, number & date fields do not.

I've searched and found a couple of routines, but none seem to work correctly with what I have.

Any ideas welcome.

Who is Participating?

Improve company productivity with a Business Account.Sign Up

ClifConnect With a Mentor Commented:
Here ya go:

    Private Function splitString(ByVal sText As String) As Array
        Dim sArr() As String
        Dim nArrItem As Integer = 0
        Dim bQuoteOpen As Boolean = False
        Dim nCount As Integer = 0

        ReDim sArr(nArrItem)

        For nCount = 0 To sText.Length - 1
            sArr(nArrItem) &= sText.Substring(nCount, 1)
            If sText.Substring(nCount, 1) = Chr(34) Then
                bQuoteOpen = Not bQuoteOpen
                If sText.Substring(nCount, 1) = " " Then
                    If Not bQuoteOpen Then
                        sArr(nArrItem) = sArr(nArrItem).Trim()
                        nArrItem += 1
                        ReDim Preserve sArr(nArrItem)
                    End If
                End If
                End If
        Next 'nCount
        Return sArr
    End Function
here is a nice example on how to do that

bealemAuthor Commented:
Hi Ronald,

That is the example I have tried.
As it is, it strips out the spaces between the "s resulting in "MrJohnSmith"

I fixed that, but can't seem to get to the items it has split, seems to make a collection type, (with keys & valies) but I don't know how to get the items out of it.
I can get it to return the number of items, but thats about it!

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

RonaldBiemansConnect With a Mentor Commented:
Hi bealem,

to call the item in the collection created, use something like this

        Dim MyString As String = "1,""My Text"",5061,""Some more, text."",839"
        Dim MySplitLine As Collection = SplitDelimitedLine(MyString, ",", """")
        MsgBox(CType(MySplitLine.Item(1), MySection).Section) '--> will return 1
        MsgBox(CType(MySplitLine.Item(2), MySection).Section) '--> will return My Text
        MsgBox(CType(MySplitLine.Item(3), MySection).Section) '--> will return 5061

oeps pressed submit too soon,

this is because the splitdelimitedline function will return a collection of mysection class
bealemAuthor Commented:
Thanks for the tips & code Ronald & Clif.

I'm going to use Clifs code, as the routine that I found & Ronald pointed out, does seem to do odd things with some data, but Ronald has sown me how to get the data out.

Clif, your code needed one minor fix:
Private Function splitString(ByVal sText As String) As String()
It seems to work perfect for what I need, thanks.

What was wrong with "As Array"?
bealemAuthor Commented:
Hi Clif,

With it set "As Array" I kept getting the error:
"Option Strict On disallows implicit conversions from 'System.Array' to '1-dimensional array of String'."
when I tried to call the splitString function, I couldn't find a way to convert (ctype) it.

I'm still pretty new to .Net, coming over from VB6, all very confusing!

I, too, have come from VB6.

I've never turned on "Option Strict".  Now I know a good reason to keep it off.  :)
bealemAuthor Commented:
Yeah, it seems far too fussy this VB.Net.

I've been on a couple of VB6 to VB.Net courses, and they always say turn on Option Strict!!!
Does help sometimes when I don't know what I'm doing, but it's a pain at other times.

Thanks for you help Clif, your code is now in use :-)

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.

All Courses

From novice to tech pro — start learning today.