个人ASP常用代码
作者:刚子 日期:2009-02-14
1.ASP获取当前网页文件名
<%
Dim fileName,arrName,postion
fileName=Request.ServerVariables("script_name")
postion=InstrRev(fileName,"/")+1
fileName=Mid(fileName,postion)
If InStr(fileName,"?")>0 Then
arrName=fileName
arrName=Split(arrName,"?")
filename=arrName(0)
End If
Response.Write filename
%>
2.ASP获取当前页URL(带参数)
<%
Function GetUrl()
Dim ScriptAddress,Servername,qs
ScriptAddress = CStr(Request.ServerVariables("SCRIPT_NAME"))
Servername = CStr(Request.ServerVariables("Server_Name"))
qs=Request.QueryString
if qs<>"" then
GetUrl ="http://"& Servername & ScriptAddress &"?"&qs
else
GetUrl ="http://"& Servername & ScriptAddress
end if
End Function
response.write GetUrl()
%>
不带参数:
<%=Request.ServerVariables("Server_Name")&Request.ServerVariables("Path_Info")%>
3.asp获取来源页面网址
<%
response.write Request.ServerVariables("HTTP_REFERER")
%>
4.ASP换行符
程序代码5.ASP获取url中问号后面的内容
<%
response.write Request.ServerVariables("QUERY_STRING")
%>
6.获取当前网址的域名,并去除前面的www (asp)
Request.ServerVariables("SERVER_NAME") '取得当前的域名,格式为www.123.com
但是前面的www.是不需要的要却掉,那么用mid函数。从左边第五个开始取。那么完整的代码就下面这个了。
<%=Mid(Request.ServerVariables("SERVER_NAME"),5)%>
7.获取当前文件所在文件夹(比如获取"www.baidu.com/1/2/3/4.php“的结果则是“1/2/3/”)
<%
path = Request.ServerVariables("PATH_INFO")
paths = split(path,"/")
folder = Replace(path,paths(ubound(paths)),"")
response.write(folder)
%>
8.ID加1
<%=(id+1)%>
9.截取网址
<%
url="http://domain.com/abc/index.asp"
domain= Mid(url,8,InStr(9,url,"/")-8)
%>
10.asp判断字符串是否包含指定字符
<%
if instr("163.com","163")>0 then
response.write("True")
end if
%>
11.asp截取函数
从左向右
<%=Mid(("123456789"),3)%>
从右向左
<%=StrReverse(Mid(StrReverse("123456789"),3))%>
12.日期函数
当期日期:<%=date()%>
当期日期减一:<%=dateadd("d",-1,date())%>
13.ASP生成订单号(格式YYYYMMDDhhmiss,配合随机数使用最好了)
<%
strNow = Now()
strNow = Year(strNow) & Right(("00" & Month(strNow)),2) & Right(("00" & Day(strNow)),2) & Right(("00" & Hour(strNow)),2) & Right(("00" & Minute(strNow)),2) & Right(("00" & Second(strNow)),2)
%>
14.SQL中日期的用法
程序代码
15.删除字符中A和B之间的字符(包含A和B - 只删除第一组)
A1="a11111111bc222222de33333333c"
程序代码
删除字符中A和B之间的字符(包含A和B - 删除所有组)
程序代码
16.split简单实例
程序代码lbound(a)=数组最小值
ubound(a)=数组做大值
17.Function初级应用
<%
Function myFunction(intA,intB)
myFunction = intA + intB
End Function
Response.write myFunction(1,2)
'输出结果将是 3
%>
18.提取字符串中所有指定位置之间的字符
程序代码
19.超简单随机数(生成1到10之间随机整数,包含1和10)
程序代码
20.将一个数组打乱它的顺序显示出来,让每一次显示出来的顺序都不一样。
程序代码
21.替换函数
程序代码
引用内容22.asp写文件
程序代码
22.去掉最后一个字符
程序代码
23.判断字符串中是否包含汉字(包括则返回true,不包括返回false)
程序代码
24.判断字符串是否全部为汉字
程序代码
25.for倒叙循环
程序代码
<%
Dim fileName,arrName,postion
fileName=Request.ServerVariables("script_name")
postion=InstrRev(fileName,"/")+1
fileName=Mid(fileName,postion)
If InStr(fileName,"?")>0 Then
arrName=fileName
arrName=Split(arrName,"?")
filename=arrName(0)
End If
Response.Write filename
%>
2.ASP获取当前页URL(带参数)
<%
Function GetUrl()
Dim ScriptAddress,Servername,qs
ScriptAddress = CStr(Request.ServerVariables("SCRIPT_NAME"))
Servername = CStr(Request.ServerVariables("Server_Name"))
qs=Request.QueryString
if qs<>"" then
GetUrl ="http://"& Servername & ScriptAddress &"?"&qs
else
GetUrl ="http://"& Servername & ScriptAddress
end if
End Function
response.write GetUrl()
%>
不带参数:
<%=Request.ServerVariables("Server_Name")&Request.ServerVariables("Path_Info")%>
3.asp获取来源页面网址
<%
response.write Request.ServerVariables("HTTP_REFERER")
%>
4.ASP换行符
程序代码<%=Chr(13)&Chr(10)%>
<%
response.write Request.ServerVariables("QUERY_STRING")
%>
6.获取当前网址的域名,并去除前面的www (asp)
Request.ServerVariables("SERVER_NAME") '取得当前的域名,格式为www.123.com
但是前面的www.是不需要的要却掉,那么用mid函数。从左边第五个开始取。那么完整的代码就下面这个了。
<%=Mid(Request.ServerVariables("SERVER_NAME"),5)%>
7.获取当前文件所在文件夹(比如获取"www.baidu.com/1/2/3/4.php“的结果则是“1/2/3/”)
<%
path = Request.ServerVariables("PATH_INFO")
paths = split(path,"/")
folder = Replace(path,paths(ubound(paths)),"")
response.write(folder)
%>
8.ID加1
<%=(id+1)%>
9.截取网址
<%
url="http://domain.com/abc/index.asp"
domain= Mid(url,8,InStr(9,url,"/")-8)
%>
10.asp判断字符串是否包含指定字符
<%
if instr("163.com","163")>0 then
response.write("True")
end if
%>
11.asp截取函数
从左向右
<%=Mid(("123456789"),3)%>
从右向左
<%=StrReverse(Mid(StrReverse("123456789"),3))%>
12.日期函数
当期日期:<%=date()%>
当期日期减一:<%=dateadd("d",-1,date())%>
13.ASP生成订单号(格式YYYYMMDDhhmiss,配合随机数使用最好了)
<%
strNow = Now()
strNow = Year(strNow) & Right(("00" & Month(strNow)),2) & Right(("00" & Day(strNow)),2) & Right(("00" & Hour(strNow)),2) & Right(("00" & Minute(strNow)),2) & Right(("00" & Second(strNow)),2)
%>
14.SQL中日期的用法
程序代码select * from key Where timec=#"&date&"# order by id desc
15.删除字符中A和B之间的字符(包含A和B - 只删除第一组)
A1="a11111111bc222222de33333333c"
程序代码s="a"
e="b"
indexa = inStr(A1,s)
indexb = inStr(A1,e)
l = indexb-indexa+1
delStr = Mid(A1,indexa,l)
B1 = replace(A1,delStr,"")
response.write B1 &"<br />"
e="b"
indexa = inStr(A1,s)
indexb = inStr(A1,e)
l = indexb-indexa+1
delStr = Mid(A1,indexa,l)
B1 = replace(A1,delStr,"")
response.write B1 &"<br />"
删除字符中A和B之间的字符(包含A和B - 删除所有组)
程序代码dim A1
A1="a11111111b222222a33333333b"
t=1
while inStr(A1,"b")>inStr(A1,"a") and inStr(A1,"a")>0 '如果b在a之右则循环
i=inStr(A1,"a") '取得a在A1中的位置
j=inStr(A1,"b") '取得b在A1中的位置
S1=Mid(A1,t,i-t) 'a以前的部分
S2=Mid(A1,j+1) 'b以后的部分
A1=S1+S2 '合并
wend
response.write a1
A1="a11111111b222222a33333333b"
t=1
while inStr(A1,"b")>inStr(A1,"a") and inStr(A1,"a")>0 '如果b在a之右则循环
i=inStr(A1,"a") '取得a在A1中的位置
j=inStr(A1,"b") '取得b在A1中的位置
S1=Mid(A1,t,i-t) 'a以前的部分
S2=Mid(A1,j+1) 'b以后的部分
A1=S1+S2 '合并
wend
response.write a1
16.split简单实例
程序代码<%
str=split("1|2|3|4|5","|")
for i=lbound(str) to ubound(str)
Response.write str(i)
next
%>
str=split("1|2|3|4|5","|")
for i=lbound(str) to ubound(str)
Response.write str(i)
next
%>
ubound(a)=数组做大值
17.Function初级应用
<%
Function myFunction(intA,intB)
myFunction = intA + intB
End Function
Response.write myFunction(1,2)
'输出结果将是 3
%>
18.提取字符串中所有指定位置之间的字符
程序代码str="开始的位置字符串结束的位置"
a="开始的位置"
b="结束的位置"
str1 = split(str,a)
num1 = ubound(str1)
for i = 1 to num1
str2 = split(str1(i),b)
response.write "" & str2(0) & "<br>"&chr(13)
next
a="开始的位置"
b="结束的位置"
str1 = split(str,a)
num1 = ubound(str1)
for i = 1 to num1
str2 = split(str1(i),b)
response.write "" & str2(0) & "<br>"&chr(13)
next
19.超简单随机数(生成1到10之间随机整数,包含1和10)
程序代码randomize
msgbox int(10*rnd+1)
msgbox int(10*rnd+1)
20.将一个数组打乱它的顺序显示出来,让每一次显示出来的顺序都不一样。
程序代码<%
Dim tt
tt=Split("1,2,3,4,5,6",",")
'随机排序
leng=UBound(tt)
randomize
for ii=0 to leng
b=int(rnd()*leng)
temp=tt(b)
tt(b)=tt(ii)
tt(ii)=temp
Next
For i=0 To leng
response.write tt(i)&"<br>"
next
%>
Dim tt
tt=Split("1,2,3,4,5,6",",")
'随机排序
leng=UBound(tt)
randomize
for ii=0 to leng
b=int(rnd()*leng)
temp=tt(b)
tt(b)=tt(ii)
tt(ii)=temp
Next
For i=0 To leng
response.write tt(i)&"<br>"
next
%>
21.替换函数
程序代码<%
content="0123456"
content = Replace(content,"123","321")
response.write content&"<br>"
%>
content="0123456"
content = Replace(content,"123","321")
response.write content&"<br>"
%>
引用内容 语法Replace ( string1, start, string2,n,m )
参数string1:string类型,指定要使用string2替换其中一部分内容的字符串;
参数start:long类型,指定要从哪个字符位置开始替换字符串,字符串中第一个字符的位置为1;
参数n:long类型,指定要替换开始字符起始位置,从左到右;
参数m:int类型,指定要替换字符串的次数,1 代表替换一次;
参数string2:string类型,指定用哪个字符串替换string1的部分字符返回值String。
参数string1:string类型,指定要使用string2替换其中一部分内容的字符串;
参数start:long类型,指定要从哪个字符位置开始替换字符串,字符串中第一个字符的位置为1;
参数n:long类型,指定要替换开始字符起始位置,从左到右;
参数m:int类型,指定要替换字符串的次数,1 代表替换一次;
参数string2:string类型,指定用哪个字符串替换string1的部分字符返回值String。
程序代码 const forreading = 1, forwriting = 2, forappending = 8 '1读取 2写入 8追加
set fso = createobject("scripting.filesystemobject")
set f = fso.opentextfile("c:\testfile.txt", forwriting, true)
f.write "hello world!"
f.close
set fso = createobject("scripting.filesystemobject")
set f = fso.opentextfile("c:\testfile.txt", forwriting, true)
f.write "hello world!"
f.close
22.去掉最后一个字符
程序代码<%
body = "123456789"
response.write left(body,len(body)-1)
%>
body = "123456789"
response.write left(body,len(body)-1)
%>
23.判断字符串中是否包含汉字(包括则返回true,不包括返回false)
程序代码<%
Function CheckIncludeChinese(strWord)
bInclude=False
for i=1 to Len(strWord)
valAsc=ASC(Mid(strWord,i,1))
If valAsc <0 or valAsc> 255 Then
bInclude=True
exit for
End If
next
CheckIncludeChinese=bInclude
End Function
%>
Function CheckIncludeChinese(strWord)
bInclude=False
for i=1 to Len(strWord)
valAsc=ASC(Mid(strWord,i,1))
If valAsc <0 or valAsc> 255 Then
bInclude=True
exit for
End If
next
CheckIncludeChinese=bInclude
End Function
%>
24.判断字符串是否全部为汉字
程序代码<%
if len(escape("刚子"))/len("刚子")=6 then
response.write "全是汉字"
end if
%>
if len(escape("刚子"))/len("刚子")=6 then
response.write "全是汉字"
end if
%>
25.for倒叙循环
程序代码for i=20 to 1 step -1
response.write i
next
response.write i
next
评论: 0 | 引用: 0 | 查看次数: -
发表评论
上一篇
下一篇

文章来自:
Tags: