您的位置:首页 > 其它

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

2008-10-17 11:47 197 查看
'函数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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐