本帖最后由 leolee82 于 2012-2-19 07:26 编辑
常用功能就要写个速度快用内存少的函数
感觉这样速度比较快也少内存,过程中只分配一次内存t = String(Len(s), " ")
mid函数也是常量复杂度的
Function zh(s, sfind, sreplace)
Dim t As String, icount As Long
If Len(s) < 3 Then zh = Replace(s, sfind, ""): Exit Function
t = String(Len(s), " ")
icount = 1
For i = 2 To Len(s)
If Mid(s, i, 1) = sfind Then
If Mid(s, i - 1, 1) <> sfind Then
Mid(t, icount, 1) = sreplace
icount = icount + 1
End If
Else
Mid(t, icount, 1) = Mid(s, i, 1)
icount = icount + 1
End If
Next
If Mid(t, icount - 1, 1) = "," Then
zh = Left(t, icount - 2)
Else
zh = Left(t, icount - 1)
End If
End Function
|