Private Sub Form_Load() MsgBox strTime("aaa1ffff1hh1", "1") End SubPrivate Function strTime(ByVal Str As String, ByVal str2 As String) As Integer Dim I As Integer Dim J As Integer J = 0 For I = 1 To Len(Str) If Mid(Str, I, 1) = str2 Then J = J + 1 End If Next strTime = J End Function
Dim Tmp() As String Dim str As String str = "aaa1ffff1hh1" Tmp = Split(str, "1") MsgBox UBound(Tmp)
Private Sub Form_Load() MsgBox getNumber("aaa1ffff1hh1", "1")End SubFunction getNumber(strSource As String, strSplit As String) As Integer Dim Tmp() As String Tmp = Split(strSource, strSplit) getNumber = UBound(Tmp) End Function
其实只要一句话就可以做到: str = "aaa1ffff1hh1"
MsgBox Len(Replace(str, "1", "11")) - Len(str)
Private Sub Command1_Click() MsgBox UBound(Split("aaa1ffff1hh1", "1")) MsgBox strnumber("aaa1ffff1hh1", "1") End Sub Function strnumber(ByVal x As String, ByVal findstr As String) As Long Dim temp As Long x = Replace(x, " ", "") temp = Len(x) x = Replace(x, findstr, "") strnumber = (temp - Len(x)) \ Len(findstr) End Function
MsgBox strTime("aaa1ffff1hh1", "1")
End SubPrivate Function strTime(ByVal Str As String, ByVal str2 As String) As Integer
Dim I As Integer
Dim J As Integer
J = 0
For I = 1 To Len(Str)
If Mid(Str, I, 1) = str2 Then
J = J + 1
End If
Next
strTime = J
End Function
Dim str As String str = "aaa1ffff1hh1"
Tmp = Split(str, "1")
MsgBox UBound(Tmp)
Dim Tmp() As String
Tmp = Split(strSource, strSplit)
getNumber = UBound(Tmp)
End Function
str = "aaa1ffff1hh1"
MsgBox Len(Replace(str, "1", "11")) - Len(str)
MsgBox UBound(Split("aaa1ffff1hh1", "1"))
MsgBox strnumber("aaa1ffff1hh1", "1")
End Sub
Function strnumber(ByVal x As String, ByVal findstr As String) As Long
Dim temp As Long
x = Replace(x, " ", "")
temp = Len(x)
x = Replace(x, findstr, "")
strnumber = (temp - Len(x)) \ Len(findstr)
End Function
=====================
但这种不通用。
高人啊,你的两种方法体现了中国人的智慧,你真是论坛的希望,相比之下,偶真是什么都不是啊~~
dim j as integer
j=0
For i = 1 To Len(Str)
If InStr(CStr(i), "1") <> 0 Then
j=j+1
end if
next