Split space delimited string with text qualifiers.

Posted on 2004-08-10
Last Modified: 2012-08-13

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.

Question by:bealem
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
  • 4
  • 3
  • 3
LVL 25

Expert Comment

ID: 11761169
here is a nice example on how to do that

Author Comment

ID: 11761210
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!

LVL 25

Assisted Solution

RonaldBiemans earned 50 total points
ID: 11761353
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

LVL 25

Expert Comment

ID: 11761361
oeps pressed submit too soon,

this is because the splitdelimitedline function will return a collection of mysection class
LVL 10

Accepted Solution

Clif earned 250 total points
ID: 11761591
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

Author Comment

ID: 11762679
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.

LVL 10

Expert Comment

ID: 11763134
What was wrong with "As Array"?

Author Comment

ID: 11763732
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!

LVL 10

Expert Comment

ID: 11764956
I, too, have come from VB6.

I've never turned on "Option Strict".  Now I know a good reason to keep it off.  :)

Author Comment

ID: 11770284
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 :-)


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor ( Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

726 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