函数:InStrC,返回字符串指定出现次数的位置。

  写东西时突然想实现一个想法,然后就百度,找来找去没找到,只好自己写了。
  其实就是一个查找一字符串在另一字符串中出现位置的函数,只是加了一个次数的判断,并非返回出现次数,而是返回你所指定的出现次数字符串所在的位置。
[CODE=vb]
Function InStrC(ByVal string1, ByVal string2, ByVal string2_count)
Dim i, count:count = 0

For i = 1 To Len(string1)
If Mid(string1, i, Len(string2)) = string2 Then count = count + 1
InStrC = i
If count = string2_count Then Exit For
Next ‘i
End Function

Response.Write InStrC(“aaaaaaabcccddeeeeeeaaaffgggggg”,”a”,9) ‘得到21
[/CODE]

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注