今天才发现这个函数的作用,原来可以查找特定的字符或者字符串。
InStr()是asp里面的一个内置函数,主要用于检测1个字符串在另外1个字符串中首次出现的位置,InStr()函数常用来检测违法字符或判断字符串是否被包含等。
InStr()函数的语法结构
InStr([start,]string1,string2[,compare])
| 参数 | 描述 |
|---|---|
| start | 可选的。规定每次搜索的起始位置。默认是搜索起始位置是第一个字符。如果已规定 compare 参数,则必须有此参数。 |
| string1 | 必需的。需要被搜索的字符串。 |
| string2 | 必需的。需搜索的字符串。 |
| compare |
必需的。规定要使用的字符串比较类型。默认是 0 。可采用下列值:
|
如果 string1 为 ""(零长度) - InStr 返回 0
如果 string1 为 Null - InStr 返回 Null
如果 string2 为 "" - InStr 返回 start
如果 string2 为 Null - InStr 返回 Null
如果 string2 没有找到 - InStr 返回 0
如果在 string1 中找到 string2,InStr 返回找到匹配字符串的位置。
如果 start > Len(string1) - InStr 返回 0
dim txt,str1 txt="我是脚本之家字符串" str1=InStr(txt,"脚") response.Write(str1)
输出结果:3
dim txt,pos txt="This is a beautiful day!" pos=InStr(txt,"his") document.write(pos)
输出结果:2
asp从1开始计算位置
dim txt,str1 txt="我是脚本之家字符串" str1=InStr(txt,"真") response.Write(str1)
输出结果:0
dim txt,str1 txt="我是脚本之家字符串,从第三个字符开始搜索本" str1=InStr(3,txt,"本",0) response.Write(str1)
输出结果:4
InStr()函数在实际项目中的应用案例
if InStr(wangzhi,".")= 0 Then goBack "网址不合法,请重新输入"
下面是我写的简单例子:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<% Response.CodePage=65001%>
<% Response.Charset="UTF-8" %>
<%
dim content_text,searchword_text
'content是内容,searchword是要搜索的文字
sub search(content,searchword)
if InStr(content,searchword) > 0 then
Response.Write("找到")
Else
Response.Write("没有找到")
End if
end sub
content_text = "脚本之家收集的代码和教程是方便自己的同时方便大家,本网站系统采用自己写的系统,系统已经写了半年时间,主要时间放在程序的优化上,程序一直都在更新,欢迎大家加入Q群一起讨论学习。"
searchword_text = "脚本之家"
call search(content_text,searchword_text)
%>