经常需要用到字符串拆分,所以弄了这个。
返回数组中下标为0的第一个元素保存拆分的字串数量,字串从数组下标1开始
''以指定分隔符把字符串拆分成若干子串
Sub StringSplit(s As String, delimit As String, result() As String)
Dim As String t
Dim As Integer p, n, count
If s <> "" Then
If delimit <> "" Then
p = Instr(s, delimit)
n = 1
Do While p <> 0
n += 1
p = Instr(p + 1,s, delimit)
Loop
If n > 1 Then
Redim result(n)
result(0) = Str(n)
t = s
For count = 1 To n
p = Instr(t, delimit)
If p = 0 Then
result(count) = t
Else
result(count) = Left(t, p-1)
Endif
t = Right(t, Len(t)-p-Len(delimit)+1)
Next
Else
Redim result(2)
result(0) = "1"
result(1) = s
Endif
Else
Redim result(2)
result(0) = "1"
result(1) = s
Endif
Else
Redim result(2)
result(0) = "1"
result(1) = s
EndIf
End Sub
''用法示例
Dim As Integer i
Dim As String Sp()
StringSplit("123asqwe1as4571as1234", "as", sp())
For i = 1 To ValInt(sp(0))
?sp(i),
Next
|
阅读(1124) | 评论(0) | 转发(0) |