ASP中如何判断一个字符串中某个指定的字符出现的次数?

2025-03-04 12:28:41
推荐回答(2个)
回答1:

判断一个字符串中某个指定的字符出现的次数,可以使用函数:【Replace】

思路:使用Replace函数将指定的字符替换为空,然后再用函数【Len】求出替换前和替换后的差值,即为出现的次数。

例如:字符串【12312345】中判断字符【2】出现的次数

dim Rc as integer
Rc=len("12312345")-len(replace("12312345","2",""))

 输出结果为:

2

回答2:

<%
str = "kdfghkwwewewdfg"
finStr = "we"
pos = 1
total = 0
while instr(pos,str,finstr)>0
total = total + 1
忘了你是要得到两个,所以这里加1就行了
pos = instr(pos,str,finstr) + 1
wend
response.write finstr & "在字符" & str & "出现了:" & total & "次"
%>