Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Split space delimited string with text qualifiers.

Posted on 2004-08-10
10
Medium Priority
?
2,846 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
ID: 11761169
here is a nice example on how to do that

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

Author Comment

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

Cheers
Mike
0
 
LVL 25

Assisted Solution

by:RonaldBiemans
RonaldBiemans earned 200 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

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 11761361
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 1000 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
            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
 

Author Comment

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

0
 
LVL 10

Expert Comment

by:Clif
ID: 11763134
bealem,
What was wrong with "As Array"?
0
 

Author Comment

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

Cheers
Mike
0
 
LVL 10

Expert Comment

by:Clif
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.  :)
0
 

Author Comment

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

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

Cheers
Mike
0

Featured Post

Independent Software Vendors: 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…
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

810 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