简体   繁体   中英

Next line on date in VB

Right now I have an entry that looks like this in a database. It's a one line entry.

HISTORY

2008-06-28 ATH: Look at your computer! 2008-07-01 ATH: Call me maybe? 2008-07-01 ATH: What are you doing? 2008-07-01 ATH: E-post: Hello, how are you?

The database has already been used for a while so its not possible to re-design the layout. What I want to do is split it up on the dates. So the result would be something like this. But I have no clue, and it's in VB.

2008-06-28 ATH: Look at your computer! 
2008-07-01 ATH: Call me maybe? 
2008-07-01 ATH: What are you doing? 
2008-07-01 ATH: E-post: Hello, how are you?

Your best option in formatting this is using a regular expression. Explanation of the regex pattern HERE .

Dim pattern as string = "\s?(\d{4}-\d{2}-\d{2}\s\w+\:)"
Dim replacement as string = vbcrlf & "$1"
Dim input as string = "2008-06-28 ATH: Look at your computer! 2008-07-01 ATH: Call me maybe? 2008-07-01 ATH: What are you doing? 2008-07-01 ATH: E-post: Hello, how are you?"

Dim rgx as new Regex(pattern)
Dim result As String = rgx.Replace(input, replacement)

The resulting string will look like this:

2008-06-28 ATH: Look at your computer!
2008-07-01 ATH: Call me maybe?
2008-07-01 ATH: What are you doing?
2008-07-01 ATH: E-post: Hello, how are you?

This code will do what you want. It uses String.IndexOf to find the "ATH:" and then extracts a SubString from the previous position to this position:

Private Function CustomStringSplit(ByVal stringToSplit As String) As List(Of String)
    Dim currentIndex As Integer
    Dim nextIndex As Integer
    Dim splitStrings As New List(Of String)

    currentIndex = stringToSplit.IndexOf("ATH: ", currentIndex) - 11
    Do While currentIndex >= 0
        nextIndex = stringToSplit.IndexOf("ATH: ", currentIndex + 12) - 11
        If nextIndex > 0 Then
            splitStrings.Add(stringToSplit.Substring(currentIndex, nextIndex - currentIndex))
        Else
            splitStrings.Add(stringToSplit.Substring(currentIndex))
        End If
        currentIndex = nextIndex
    Loop
    Return splitStrings
End Function

Usage:

    Dim s As String = "2008-06-28 ATH: Look at your computer! 2008-07-01 ATH: Call me maybe? 2008-07-01 ATH: What are you doing? 2008-07-01 ATH: E-post: Hello, how are you?"

    For Each split As String In CustomStringSplit(s)
        Debug.WriteLine(split)
    Next

Output:

2008-06-28 ATH: Look at your computer! 
2008-07-01 ATH: Call me maybe? 
2008-07-01 ATH: What are you doing? 
2008-07-01 ATH: E-post: Hello, how are you?

However, this is really bad db design; these should be rows in a table not all in one column value, because your next question is going to be "how do I parse the start of this string into a date?"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM