Chinaunix首页 | 论坛 | 博客
  • 博客访问: 182065
  • 博文数量: 14
  • 博客积分: 1403
  • 博客等级: 上尉
  • 技术积分: 192
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-18 16:36
文章分类

全部博文(14)

文章存档

2011年(5)

2010年(9)

分类: WINDOWS

2010-05-18 09:47:07


经常需要用到字符串拆分,所以弄了这个。

返回数组中下标为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) |
给主人留下些什么吧!~~