网页素材: 导航菜单选项卡滑动门焦点图图片切换下拉菜单弹出层Tabs链接提示

您的当前位置: 源码爱好者 » 网页制作教程(编程教程) » VB技术教程

两种方法Vb字符串替换 连在一起的多字符替换

  • 标签:字符串替换 更新时间:2014-07-23
  • VB替换字符串,把一个字符串中的子字符串替换成另一个字符串,实现方法有很多啦,这里列出两个比较常见且简单的方法。

    VB字符串替换方法一:

    Public Function FastReplace(SSrch$, SFind$, SRepl$) As String
    Dim Src() As Byte, Dst() As Byte, R() As Byte, F() As Byte
    Dim LenF&, LenR&, LenDst&, i&, j&, OutPos&
    Const ChunkSize& = 4096
    If SSrch = "" Or SFind = "" Then Exit Function
    Src = SSrch: F = SFind: R = SRepl
    LenF = UBound(F): LenR = UBound(R)
    LenDst = ChunkSize: ReDim Dst(0 To LenDst - 1)
    For i = 0 To UBound(Src) Step 2
    For j = 0 To LenF Step 2
    If Src(i + j) <> F(j) Then Exit For
    Next j
    If j > LenF Then 'Found
    For j = 0 To LenR Step 2
    If OutPos >= LenDst Then
    LenDst = LenDst + ChunkSize
    ReDim Preserve Dst(0 To LenDst)
    End If
    Dst(OutPos) = R(j): OutPos = OutPos + 2
    Next j
    i = i + LenF - 1
    Else
    If OutPos >= LenDst Then
    LenDst = LenDst + ChunkSize
    ReDim Preserve Dst(0 To LenDst)
    End If
    Dst(OutPos) = Src(i): OutPos = OutPos + 2
    End If
    Next i
    ReDim Preserve Dst(0 To OutPos - 2): SSrch = Dst
    FastReplace = SSrch$
    End Function

    Vb字符串替换方法二:

    'SearchLine is input, SearchFor is what to search for, ReplaceWith is the replacement
    Function sReplace(SearchLine As String, SearchFor As String, ReplaceWith As String)
    Dim vSearchLine As String, found As Integer
    found = InStr(SearchLine, SearchFor): vSearchLine = SearchLine
    If found <> 0 Then
    vSearchLine = ""
    If found > 1 Then vSearchLine = Left(SearchLine, found - 1)
    vSearchLine = vSearchLine + ReplaceWith
    If found + Len(SearchFor) - 1 < Len(SearchLine) Then _
    vSearchLine = vSearchLine + Right$(SearchLine, Len(SearchLine) - found - Len(SearchFor) + 1)
    End If
    sReplace = vSearchLine
    End Function

    两种方法你可二选一,可根据你的实际程序环境选择使用,当然你也可在上述两代码基础上继续修改完善。

  • 已读: 次 收藏本文关闭本文打印本文复制链接
处理 SSI 文件时出错