?
Solved

Split space delimited string with text qualifiers.

Posted on 2004-08-10
10
Medium Priority
?
2,814 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
[X]
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
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

752 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