【发布时间】:2019-08-29 10:44:35
【问题描述】:
我有一个搜索框。
我的管理员用户可能会搜索“@MG @EB dorchester”。
在 ASP 中,我需要计算符号“@”在字符串中出现的次数。怎么可能?
【问题讨论】:
标签: vbscript asp-classic
我有一个搜索框。
我的管理员用户可能会搜索“@MG @EB dorchester”。
在 ASP 中,我需要计算符号“@”在字符串中出现的次数。怎么可能?
【问题讨论】:
标签: vbscript asp-classic
试试这个:
len(yourString) - len(replace(yourString, "@", ""))
【讨论】:
len(yourString) - len(replace(yourString, "@", "")) / len(substring)
Response.write ubound(split(str,"@"))
足以计算特定字符的出现次数
【讨论】:
对于 JW01
Dim pos : pos = 0
Dim count : count = -1
Do
count = count + 1
pos = InStr(pos + 1, str, "@")
Loop While (pos > 0)
【讨论】:
Replace 函数中所需的新大字符串的创建和复制将比此答案中的Do..Loop 花费更多。但是我怀疑“大”比典型的要多得多,所以对于典型的脚本,安德鲁的更好。
尝试一个while循环:
Do While (str.indexOf("@") != -1)
count = count + 1
str = right(str, len(str) - str.indexOf("@"))
Loop
编辑:
这个 for 循环可能更有意义:
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
【讨论】:
str 变量,这可能是一个不需要的副作用。
将搜索替换为空白,找出原始字符串和新字符串之间的差异将是字符串出现的次数
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
【讨论】: