两款查找替换所选字符函数

 '函数1   只能查找替换
' SearchLine is input, SearchFor is what to search for, ReplaceWith is the replacement
'--------------------------------------
'函数名:sReplace                     --
'输入: SearchLine=字符串             --
'      SearchFor=要查找的字符串      --
'      ReplaceWith=替换的字符串      --
'输出: sReplace=替换后的字符串       --
'描述: 在全字符串中查找替换字符      --
'作者:                               --
'--------------------------------------
Function sReplace(SearchLine As String, SearchFor As String, ReplaceWith As String)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

'函数2   '1不设置查找字符串时,可以追加字符串,可以设置是否区分大小写查找。
'******************************************************************
'**函 数 名:Substitute
'**输    入:
'**          Expression   字符串表达式
'**          FindStr      要搜索的子字符串。
'**                       如果为空,就在原有字符串后面加上替换字符。
'**          ReplaceStr   用来替换的子字符串
'**          Start        搜索的开始位置。忽略,从1开始。
'**          DivideSize   是否区分大小写,忽略,区分大小写。
'**输    出:Substitute   替换后的字符串
'**描    述:在全字符串中查找替换字符,如果不指定查找,就添加。
'**作    者:陈峰
'**日    期:2008-10-05
'*******************************************************************
Function Substitute(Expression As String, _
                  FindStr As String, ReplaceStr As String, _
                  Optional Start As Long = 1, _
                  Optional DivideSize As Boolean = True) As String
    Dim StrText As String, TextWZ As Long
    If FindStr <> "" Then
        If DivideSize = False Then
            TextWZ = InStr(Start, LCase(Expression), LCase(FindStr), vbTextCompare)
        Else
            TextWZ = InStr(Start, Expression, FindStr, vbTextCompare)
        End If
        If TextWZ <> 0 Then
            StrText = Left(Expression, TextWZ - 1) & ReplaceStr
            StrText = StrText & Right(Expression, Len(Expression) - (TextWZ + Len(FindStr) - 1))
        End If
    Else
        StrText = Expression & ReplaceStr
    End If
    Substitute = StrText
End Function

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值