现在我在数据库中有一个看起来像这样的条目。这是一个单行条目。
记录
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?
数据库已经使用了一段时间,因此无法重新设计布局。 我想要做的是在日期分开。所以结果将是这样的。但我不知道,这是在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?
答案 0 :(得分:0)
格式化的最佳选择是使用正则表达式。正则表达式模式的解释 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)
结果字符串将如下所示:
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?
答案 1 :(得分:0)
此代码将执行您想要的操作。它使用String.IndexOf查找“ATH:”,然后从前一个位置提取一个SubString到这个位置:
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
用法:
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
输出:
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?
然而,这是非常糟糕的数据库设计;这些应该是表中的行,而不是一列中的值,因为您的下一个问题是“如何将此字符串的开头解析为日期?”