Private Sub Command1_Click() Dim st As String, MyArrary() As String, i As Long, Number As String, Price As String, Po As Long '是一个不定长度的字符串,如下: st = "51020003#0.01*51020003#7.5*51020004#1*51020004#100*51020005#0.1*" MyArrary = Split(st, "*")
For i = 0 To UBound(MyArrary) Debug.Print MyArrary(i) Po = InStr(1, MyArrary(i), "#") - 1 If Po > 0 Then Number = Number & "|" & Mid$(MyArrary(i), 1, Po) Price = Price & "|" & Right$(MyArrary(i), Len(MyArrary(i)) - InStr(1, MyArrary(i), "#")) Next Debug.Print Number, Price End Sub
Private Sub Command1_Click() Dim st As String st = "51020003#0.01*51020003#7.5*51020004#1*51020004#100*51020005#0.1*"
Dim tmp1() As String, tmp2() As String, tmp3() As String tmp1 = Split(st, "*") ReDim Preserve tmp1(UBound(tmp1) - 1) ReDim tmp2(UBound(tmp1)) ReDim tmp3(UBound(tmp2))
Dim i As Integer, j As Integer Dim jtmp1 As String
For i = 0 To UBound(tmp1) tmp2(i) = Split(tmp1(i), "#")(0) tmp3(i) = Split(tmp1(i), "#")(1) Next
For i = 0 To UBound(tmp1) - 1 If tmp2(i) = tmp2(i + 1) Then tmp2(i) = "" tmp3(i) = tmp3(i) & "/" Else tmp3(i) = tmp3(i) & "|" End If Next
居然没人用正则…… 添加引用: Microsoft VBScript Regular Expressions 5.5 然后用如下代码Private Sub Command1_Click() Dim st As String '是一个不定长度的字符串,如下: st = "51020003#0.01*51020003#7.5*51020004#1*51020004#100*51020005#0.1*" '使用正则匹配 Dim mc As MatchCollection Set mc = Matches("(\d+)#(\d+(?:\.\d+)?)", st) Dim m As Match Dim s1 As String Dim s2 As String For Each m In mc s1 = s1 & m.SubMatches(0) & "|" s2 = s2 & m.SubMatches(1) & "|" Next Dim s As String s = Left(s1, Len(s1) - 1) & "," & Left(s2, Len(s2) - 1) MsgBox s End SubPublic Function Matches(ByVal pattern As String, ByVal test As String) As MatchCollection '定义正则 Dim myRegExp As RegExp Dim myMatches As MatchCollection Set myRegExp = New RegExp myRegExp.IgnoreCase = True myRegExp.Global = True '正则表达式 myRegExp.pattern = pattern '匹配 Set myMatches = myRegExp.Execute(test) Set Matches = myMatches End Function
Dim st As String, MyArrary() As String, i As Long, Number As String, Price As String, Po As Long
'是一个不定长度的字符串,如下:
st = "51020003#0.01*51020003#7.5*51020004#1*51020004#100*51020005#0.1*"
MyArrary = Split(st, "*")
For i = 0 To UBound(MyArrary)
Debug.Print MyArrary(i)
Po = InStr(1, MyArrary(i), "#") - 1
If Po > 0 Then Number = Number & "|" & Mid$(MyArrary(i), 1, Po)
Price = Price & "|" & Right$(MyArrary(i), Len(MyArrary(i)) - InStr(1, MyArrary(i), "#"))
Next
Debug.Print Number, Price
End Sub
Private Sub Command1_Click()
Dim st As String
st = "51020003#0.01*51020003#7.5*51020004#1*51020004#100*51020005#0.1*"
Dim tmp1() As String, tmp2() As String, tmp3() As String
tmp1 = Split(st, "*")
ReDim Preserve tmp1(UBound(tmp1) - 1)
ReDim tmp2(UBound(tmp1))
ReDim tmp3(UBound(tmp2))
Dim i As Integer, j As Integer
Dim jtmp1 As String
For i = 0 To UBound(tmp1)
tmp2(i) = Split(tmp1(i), "#")(0)
tmp3(i) = Split(tmp1(i), "#")(1)
Next
For i = 0 To UBound(tmp1) - 1
If tmp2(i) = tmp2(i + 1) Then
tmp2(i) = ""
tmp3(i) = tmp3(i) & "/"
Else
tmp3(i) = tmp3(i) & "|"
End If
Next
jtmp1 = Replace(Join(tmp2, "|") & "," & Join(tmp3), "||", "|")
MsgBox jtmp1
End Sub
添加引用:
Microsoft VBScript Regular Expressions 5.5
然后用如下代码Private Sub Command1_Click()
Dim st As String
'是一个不定长度的字符串,如下:
st = "51020003#0.01*51020003#7.5*51020004#1*51020004#100*51020005#0.1*" '使用正则匹配
Dim mc As MatchCollection
Set mc = Matches("(\d+)#(\d+(?:\.\d+)?)", st)
Dim m As Match
Dim s1 As String
Dim s2 As String
For Each m In mc
s1 = s1 & m.SubMatches(0) & "|"
s2 = s2 & m.SubMatches(1) & "|"
Next
Dim s As String
s = Left(s1, Len(s1) - 1) & "," & Left(s2, Len(s2) - 1)
MsgBox s
End SubPublic Function Matches(ByVal pattern As String, ByVal test As String) As MatchCollection
'定义正则
Dim myRegExp As RegExp
Dim myMatches As MatchCollection
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Global = True
'正则表达式
myRegExp.pattern = pattern
'匹配
Set myMatches = myRegExp.Execute(test)
Set Matches = myMatches
End Function