Solved

Split space delimited string with text qualifiers.

Posted on 2004-08-10
10
2,767 Views
Last Modified: 2012-08-13
Hi,

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.
e.g:
"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.

Cheers
Mike
0
Comment
Question by:bealem
  • 4
  • 3
  • 3
10 Comments
 
LVL 25

Expert Comment

by:RonaldBiemans
Comment Utility
here is a nice example on how to do that

http://www.freevbcode.com/ShowCode.asp?ID=4938
0
 

Author Comment

by:bealem
Comment Utility
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!

Cheers
Mike
0
 
LVL 25

Assisted Solution

by:RonaldBiemans
RonaldBiemans earned 50 total points
Comment Utility
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

0
 
LVL 25

Expert Comment

by:RonaldBiemans
Comment Utility
oeps pressed submit too soon,

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

Accepted Solution

by:
Clif earned 250 total points
Comment Utility
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
            Else
                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
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:bealem
Comment Utility
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.

0
 
LVL 10

Expert Comment

by:Clif
Comment Utility
bealem,
What was wrong with "As Array"?
0
 

Author Comment

by:bealem
Comment Utility
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!

Cheers
Mike
0
 
LVL 10

Expert Comment

by:Clif
Comment Utility
I, too, have come from VB6.

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

Author Comment

by:bealem
Comment Utility
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.

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

Cheers
Mike
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 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

15 Experts available now in Live!

Get 1:1 Help Now