Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse text with dashes and dates 1

Status
Not open for further replies.

ynnepztem

Programmer
Aug 2, 2001
54
US
I have this string
2/1/2012 - 321 2/1/2012 - 213 2/1/2012 - 21 2/1/2012 - 321 2/1/2012 - 231 2/1/2012 - 321 2/1/2012 - l;k;l 2/1/2012 - 546 2/1/2012 - 545 2/1/2012 - 45 2/1/2012 - kl; 2/1/2012 - 55464 1/18/2012 - added web2 1/18/2012 - added falcon 1/17/2012 -
that I want to parse to this result.
2/1/2012 - 321
2/1/2012 - 213
2/1/2012 - 21
2/1/2012 - 321
2/1/2012 - 231
2/1/2012 - 321
2/1/2012 - lmkml
2/1/2012 - 546
2/1/2012 - 545
2/1/2012 - 45
2/1/2012 - klk
2/1/2012 - 55464
1/18/2012 - added web
2 1/18/2012 - added falcon
1/17/2012 -

This is a history file from changes to an application. (Date dash info)

 
A few things. How are you getting the string in the first place? Next, do you know for sure the data will always be in this format? No 02/01/2012 dates, always date first, etc? The last item has nothing after the dash. Can that happen or did you just cut off your example too early? Finally what do you want to do with the data after you parse it?

Not knowing all of that here is one way. I admit it is kind of sloppy, but this was the quickest way I could think of without knowing more.

Code:
    Private Sub test()
        Dim pStr As String = "2/1/2012 - 321 2/1/2012 - 213 2/1/2012 - 21 2/1/2012 - 321 2/1/2012 - 231 2/1/2012 - 321 2/1/2012 - l;k;l 2/1/2012" & _
                            "- 546 2/1/2012 - 545 2/1/2012 - 45 2/1/2012 - kl; 2/1/2012 - 55464 1/18/2012 - added web2 1/18/2012 - added falcon 1/17/2012 -"
        Dim pSplit As String() = pStr.Split(" ")
        Dim colDate As New Collection
        Dim BuildStr As String = ""

        For count As Integer = 0 To pSplit.Length - 1
            Dim strCurrent As String = pSplit(count)

            If strCurrent.Contains("/") Or count = pSplit.Length - 1 Then
                If BuildStr <> "" Then colDate.Add(BuildStr.Trim)
                BuildStr = ""
            End If

            BuildStr &= strCurrent & " "
        Next

        For Each curstr As String In colDate
            ListBox1.Items.Add(curstr)
        Next
    End Sub

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Oops. You don't need the last part in there.
Code:
        For Each curstr As String In colDate
            ListBox1.Items.Add(curstr)
        Next

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Thanks for your reply. The data is from an application I am writing that creates two documents when our mainframe programmers complete their applications. Prior to moving them to production, they have to use my application to gather all of the required data. If they go in to do any edits, they have to put a statement in the "History" box. I start them off with the current date and a dash. That's why there is a dash hanging in there. I strip that off later when I refill the form. All of the history data is stored in one long string (I've since changed the way I gather the history so I don't really need the code in your reply for this anymore, but I am putting it in my "How To's" folder for future reference).
 
I seriously need more sleep. That isn't right because that will always leave the last item off. In this instance that would be a blank so it isn't noticeable. Here.

Code:
    Private Sub test()
        Dim pStr As String = "2/1/2012 - 321 2/1/2012 - 213 2/1/2012 - 21 2/1/2012 - 321 2/1/2012 - 231 2/1/2012 - 321 2/1/2012 - l;k;l 2/1/2012" & _
                            "- 546 2/1/2012 - 545 2/1/2012 - 45 2/1/2012 - kl; 2/1/2012 - 55464 1/18/2012 - added web2 1/18/2012 - added falcon 1/17/2012 -"
        Dim pSplit As String() = pStr.Split(" ")
        Dim colDate As New Collection
        Dim BuildStr As String = ""

        For count As Integer = 0 To pSplit.Length - 1
            Dim strCurrent As String = pSplit(count)
            Dim LastBuild As String = ""

            If strCurrent.Contains("/") Then
                LastBuild = BuildStr
                BuildStr = ""
            End If

            BuildStr &= strCurrent & " "

            If count = pSplit.Length - 1 Then
                LastBuild = BuildStr
            End If

            If LastBuild <> "" Then
                colDate.Add(LastBuild.Trim)
            End If
        Next
    End Sub

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
I put your code to the test and it did exactly what I asked for. Thanking you for future projects!
 
Just saw your message. Cool. I'm half awake and it really annoyed me I couldn't think it through right.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top