2011-09-15 139 views

回答

34

試試這個:

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

不錯 - 非常優雅! – JW8

+0

甜,謝謝:) – TheCarver

+0

我想計算一個字符串的出現次數超過一個字符,所以我用你的答案添加了「/ len(substring)」。作品。謝謝:) – MrVimes

1

嘗試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 
+0

感謝您的意見,但我認爲我更喜歡安德魯的嘗試 – TheCarver

+0

嗯......如果str包含一個「@」什麼時候完成? – AnthonyWJones

+0

@安東尼,好抓!是的,將不得不修改字符串以避免無限循環。當我回答問題時有點遲了。 – JW8

4

對於JW01

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

嗨安東尼。感謝您的輸入。你能澄清一下安德魯的建議是什麼問題,它顯然至少分配了2個字符串?您提供的代碼是否會更快處理? – TheCarver

+1

@Martin:Andrew以任何真實或實用的方式提出建議都沒有問題。我發佈了這個答案,因爲我認爲JW01的最初嘗試有一些優點,但實施有缺陷。這種方法的唯一優點是要搜索的字符串很大。在某種程度上,在這個答案中,創建和複製Andrew所使用的'Replace'函數所需的新大字符串將比'Do..Loop'花費更多。然而我懷疑「大」比典型的要多得多,因此對於典型的腳本來說,安德魯斯更好。 – AnthonyWJones

0

替換空白搜索,找到之間的和原始的和新的字符串的差異將時間的數字的字符串存在

Dim a = "I @ am @ [email protected]" 
Dim count 
count = Len(a) - Len(Replace(a,"@","")) 
Response.write count 
0
Response.write ubound(split(str,"@")) 

是足夠的計數次數的特定字符