a)通过第一分割动作获得相同的 day date(dayPrefix),并将该字符串定义为分割 delimiter
B)将略有修改的输入字符串s拆分为time令牌
c)删除起始分割元素,因为它自动为空
d)返回所有时间令牌的从零开始的数组
Function GetAllTimeTokens(ByVal s As String)
'Purp: isolate all time tokens (within pairs of date time list)
'Note: assumes same day date for all time indications
Const DEL = "$DEL$"
'a) get day prefix
Dim dayPrefix As String
dayPrefix = Split(s, " ")(0)
'b) split into time parts
s = Replace(Replace(s, "--", ""), ",", "")
Dim tmp As Variant
tmp = Split(s, dayPrefix & " ")
'c) remove starting split element (being empty)
tmp(0) = DEL: tmp = Filter(tmp, DEL, False)
'd) return function result
GetAllTimeTokens = tmp
End Function
调用示例
Sub ExampleCall()
Dim s As String
s = "2023-05-16 06:55--2023-05-16 07:05,2023-05-16 08:45--2023-05-16 09:15,2023-05-16 11:00--2023-05-16 11:10"
Dim arr As Variant
arr = GetAllTimeTokens(s) ' << returns zero-based 1D array of paired time tokens
'Output in VB Editor's immediate windo
Debug.Print "~~> " & Join(arr, "|")
'~~> 06:55|07:05|08:45|09:15|11:00|11:10
End Sub
Private Sub GetTimes(ByVal ComplexValue As String)
Dim Entries As Variant
Dim Entry As Variant
Dim DateAndTimes As Variant
Dim DateAndTime As Variant
Dim Time As Variant
Entries = Split(ComplexValue, ",")
For Each Entry In Entries
DateAndTimes = Split(Entry, "--")
For Each DateAndTime In DateAndTimes
Time = Split(DateAndTime, " ")(1)
Debug.Print Time
Next
Next
End Sub
2条答案
按热度按时间p4rjhz4m1#
按照示例字符串的结构,您列出了一天内的停止时间对,其中
"--"
组合了所有的开始-结束值,而","
分隔了每一对。假设任何 * 对 * 列出的起止时间都是 * 同一天日期 *,您可以尝试以下操作:
dayPrefix
),并将该字符串定义为分割 delimiters
拆分为time令牌调用示例
根据您的要求使用生成的基于0的1D数组
arr
,例如索引对0..1
将指示arr(0)
作为第一开始时间06:55
,而arr(1)
将显示随后的结束时间07:05
,等等。对于所有偶数/奇数索引对。gk7wooem2#
RegEx当然是解决这个问题的好方法。你最初的方法是正确的,所以为了你的利益,你可以这样做: