计算字符串中的特定字符数

时间:2011-09-15 00:32:01

标签: vbscript asp-classic

我有一个搜索框。

我的管理员用户可能会搜索“@MG @EB dorchester”。

在ASP中,我需要计算符号“@”出现在字符串中的次数。怎么可能?

6 个答案:

答案 0 :(得分:38)

试试这个:

len(yourString) - len(replace(yourString, "@", ""))

答案 1 :(得分:4)

对于JW01

Dim pos : pos = 0
Dim count : count = -1
Do 
  count = count + 1
  pos = InStr(pos + 1, str, "@")
Loop While (pos > 0)

答案 2 :(得分:2)

尝试一下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

答案 3 :(得分:1)

Response.write ubound(split(str,"@"))

足以计算特定字符的出现次数

答案 4 :(得分:0)

将搜索替换为空白,找出原始字符串和新字符串之间的差异将是字符串存在的时间

Dim a = "I @ am @ Thirs@ty" 
Dim count 
count = Len(a) - Len(Replace(a,"@",""))
Response.write count

答案 5 :(得分:0)

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