两款查找替换所选字符函数
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
' 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
相关文章推荐
- [C/C++]_[初级]_[标准库之几种算法库函数的运用,如字符串查找(字符个数查找)、分隔、删除、替换、排序、字符交换位置、大小写转换等等]
- Oracle 使用TRANSLATE函数替换字符串中的字符
- SQL中的REPLACE替换字符函数的用法(使之支持text/ntext)
- 字符串替换空格: 请实现一个函数,把字符数组中的每个空格替换成“%20”。 例如输入“we are happy.”,则输出“we are happy.”
- C++基础-string截取、替换、查找子串函数
- 转贴 有关js 函数验证和字符串查找替换的方法
- 关于动态存储分配函数的调用,在已经过排序的数组中查找及删除内容的操作,余数的分析,删除字符数组中的空格,对链表的逆置,在源字符串中查找子字符串的个数,函数指针以及函数的调用,循环赋值带来的问题以及插入
- JSP中有关时间和日期类的使用与JSP中的字符替换函数 str_replace()实现
- JS 字符串操作函数 往指定位置插入字符 删除指定位置字符 替换指定位置字符
- 请编写一个函数,它在一个字符串中进行搜索,查找所有 在一个给定字符集合中出现的字符。
- js中字符替换函数String.replace()使用技巧
- delphi 字符串查找替换函数
- 请实现一个函数,把字符数组中的每个空格替换成“%20”。 例如输入“we are happy.”,则输出“we%20are%20happy.”。
- string类字符串中的两个查找字符函数
- js中字符替换函数String.replace()使用技巧
- 字符串中字符查找函数:strchr,wcschr 及strrchr, wcsrchr函数
- JS字符替换函数
- 几个范畴中的字符替换(正则表达式和函数的结合运用)
- 写一个函数查找每个字符的个数,区分大小写,时间复杂度要求O(n)
- replace MYSQL字符替换函数sql语句分享(正则判断)