字符串转数组 字符串中包含某个字符的个数
'返回某一字符串在另一个字符串中出现的次数 index返回出现的位置数组
Public Function inStr_n(str As String, StrIn As String, Optional index As Variant) As Long
Dim i As Long
Dim Temp As Long: Temp = 1
Dim N As Long
N = 0
For i = 1 To Len(str)
Temp = InStr(Temp + 1, str, StrIn)
If Temp = 0 Then
Exit For
Else
If IsMissing(index) = False Then
ReDim Preserve index(N)
index(N) = Temp
End If
N = N + 1
End If
Next i
inStr_n = N
End Function
'***************************************************
'字符串(默认空格为分隔符)转变为数组或empty
'***************************************************
Function StoDim(ByVal S As String, Optional div As String) As Variant
Dim s_len As Integer '字符串长度
Dim s_p As Integer '查找开始位置
Dim gs() As String
Dim i As Integer
Dim j As Integer
If div = "" Then div = " "
i = 0
s_p = 1
S = LTrim(S + div)
s_len = Len(S)
j = 0
While s_p <= s_len '找到最后子串
If Mid(S, s_p, 1) = div Then '如果找到分隔符
'取子字符串
If s_p > 1 Then
ReDim Preserve gs(j)
gs(j) = Left(S, s_p - 1)
j = j + 1
End If
S = LTrim(Right(S, s_len - s_p))
s_len = Len(S) '替换后新串长度
s_p = 1 '下次开始查找的位置
i = i + 1
Else
s_p = s_p + 1 '如果没有找分隔符,从下一个开始
End If
Wend
'空数组
If j = 0 Then Exit Function
StoDim = gs '得到字符串数组
End Function
Public Function inStr_n(str As String, StrIn As String, Optional index As Variant) As Long
Dim i As Long
Dim Temp As Long: Temp = 1
Dim N As Long
N = 0
For i = 1 To Len(str)
Temp = InStr(Temp + 1, str, StrIn)
If Temp = 0 Then
Exit For
Else
If IsMissing(index) = False Then
ReDim Preserve index(N)
index(N) = Temp
End If
N = N + 1
End If
Next i
inStr_n = N
End Function
'***************************************************
'字符串(默认空格为分隔符)转变为数组或empty
'***************************************************
Function StoDim(ByVal S As String, Optional div As String) As Variant
Dim s_len As Integer '字符串长度
Dim s_p As Integer '查找开始位置
Dim gs() As String
Dim i As Integer
Dim j As Integer
If div = "" Then div = " "
i = 0
s_p = 1
S = LTrim(S + div)
s_len = Len(S)
j = 0
While s_p <= s_len '找到最后子串
If Mid(S, s_p, 1) = div Then '如果找到分隔符
'取子字符串
If s_p > 1 Then
ReDim Preserve gs(j)
gs(j) = Left(S, s_p - 1)
j = j + 1
End If
S = LTrim(Right(S, s_len - s_p))
s_len = Len(S) '替换后新串长度
s_p = 1 '下次开始查找的位置
i = i + 1
Else
s_p = s_p + 1 '如果没有找分隔符,从下一个开始
End If
Wend
'空数组
If j = 0 Then Exit Function
StoDim = gs '得到字符串数组
End Function
[本日志由 田草 于 2008-01-18 12:44 AM 编辑]
|
暂时没有评论
发表评论 - 不要忘了输入验证码哦! |