Count how many specific characters in string
I have a search box.
My admin user mig开发者_开发问答ht search for "@MG @EB dorchester".
In ASP, I need to count how many times the symbol "@" appears in the string. How is the possible?
Try this:
len(yourString) - len(replace(yourString, "@", ""))
Response.write ubound(split(str,"@"))
is enough for counting the occurance of a specific character
For JW01
Dim pos : pos = 0
Dim count : count = -1
Do
count = count + 1
pos = InStr(pos + 1, str, "@")
Loop While (pos > 0)
Try a while loop:
Do While (str.indexOf("@") != -1)
count = count + 1
str = right(str, len(str) - str.indexOf("@"))
Loop
EDIT:
This for loop might make more sense:
dim strLen, curChar, count
count = 0
int strLen = len(str)
for i = 1 to strLen
curChar = mid(str, i, 1)
if curChar = "@"
count = count + 1
end if
next
Replace the search with blank and find the difference between and original and new string will the number of time a string is present
Dim a = "I @ am @ Thirs@ty"
Dim count
count = Len(a) - Len(Replace(a,"@",""))
Response.write count
Function FnMatchedStringCountFromText(strText,strStringToSearch)
strLength = Len(strText)
strNumber = 1
IntCount = 0
For i = 1 to strLength
If Instr(1,strText,strStringToSearch,0) > 0 Then
stMatch = Instr(1,strText,strStringToSearch,0)
strText = Mid(strText,stMatch+2,strLength)
IntCount = IntCount+1
Else
Exit For
End If
Next
FnMatchedStringCountFromText = IntCount
End Function
精彩评论