Function
replace
(ByVal sstr
As
String
, ByVal stag
As
String
, ByVal srep
As
String
)
As
String
Dim
l1, l2, l3, x, i
As
Long
Dim
st
As
String
x
=
InStr
(sstr, stag)
If
x
<
1
Then
replace
=
sstr
Exit
Function
End
If
st
=
sstr
l1
=
Len
(sstr)
l2
=
Len
(stag)
l3
=
Len
(srep)
For
i
=
0
To
l1
st
=
Left
(st, x
-
1
)
&
srep
&
Right
(st,
Len
(st)
-
x
-
l2
+
1
)
x
=
InStr
(x
+
l3, st, stag)
If
x
<
1
Then
Exit
For
Next
replace
=
st
End Function
Function
split
(ByVal sstr
As
String
, ByVal spstr
As
String
)
As
Variant
Dim
star, lenstr, lensp, cur
As
Integer
Dim
backstr()
As
String
Dim
i
As
Integer
ReDim
backstr(
0
)
lenstr
=
Len
(sstr)
lensp
=
Len
(spstr)
star
=
InStr
(sstr, spstr)
If
star
<
1
Then
backstr(
0
)
=
sstr
split
=
backstr()
Exit
Function
End
If
backstr(
0
)
=
Left
(sstr, star
-
1
)
cur
=
star
+
lensp
For
i
=
star
+
lensp
To
lenstr
star
=
InStr
(star
+
lensp, sstr, spstr)
If
star
>
0
Then
ReDim
Preserve backstr(
UBound
(backstr)
+
1
)
backstr(
UBound
(backstr))
=
Mid
(sstr, cur, star
-
cur)
cur
=
star
+
lensp
Else
Exit
For
End
If
Next
ReDim
Preserve backstr(
UBound
(backstr)
+
1
)
backstr(
UBound
(backstr))
=
Mid
(sstr, cur, lenstr
-
cur
+
1
)
split
=
backstr()
End Function
temptation 2007-09-07 14:22
发表评论
本文介绍了两个实用的VBA函数:replace和split。replace函数用于替换字符串中的指定内容,而split函数则负责按指定分隔符拆分字符串。通过这两个函数,可以高效地进行字符串操作。
1459

被折叠的 条评论
为什么被折叠?



