几个常用的asp自造函数
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
[p]getrepeattimes(thechar,thestring) 得到一个字符串在另一个字符串当中出现几次的函数(新)[br][br]如:[br]response.write getrepeattimes("w",aspxhome.com) [br]response.write getrepeattimes("ww","wwwww")[br]在网上看到过一个checkthechar(thechar,thestring)函数,有个bug,在检测wwwww中有几个ww时,会错误的返回4个![br][br]cleft(string, length) 返回指定数目的从字符串的左边算起的字符,区分单双字节。[br][br]如:[br]dim mystring, leftstring [br]mystring = "文字测试vbscript" [br]leftstring = cleft(mystring, 10) [br]返回 "文字测试vb"。[br][br][br]myrandc(n) 生成随机字符,n为字符的个数[br][br]如:[br]response.write myrandn(10)[br]输出10个随机字符[br][br][br]myrandn(n) 生成随机数字,n为数字的个数[br][br]如:[br]response.write myrandn(10)[br]输出10个随机数字[br][br][br]formatquerystr(str) 格式化sql中的like字符串.[br]如:[br]q = request("q") [br]q = formatquerystr(q) [br]sql = "select * from [table] where aa like ’%"& q &"%’"[br]getrnd(min,max) 返回min - max之间的一个随机数[br][br]如:[br]response.write getrnd(100,200)[br]输出大于100到200之间的一个随机数[br][br]regreplace(str,regexstr,repalcestr) 对str 进行正则替换[br][br]如:[br]htmlstr = "123456" [br]htmlstr2 = regreplace(htmlstr,"<(.[^><]*)>","")[br]返回 htmlstr2 为123456[br][br]所有函数如下:[br]function cleft(str,n)[br]dim str1,str2,alln,islefted[br]str2 = ""[br]alln = 0[br]str1 = str[br]islefted = false[br]if isnull(str) then[br] cleft = ""[br] exit function[br]end if[br]for i = 1 to len(str1)[br] nowstr = mid(str1,i,1)[br] if asc(nowstr)<0 then[br] alln = alln + 2[br] else[br] alln = alln + 1[br] end if[br] if (alln<=n) then[br] str2 = str2 & nowstr[br] else[br] islefted = true[br] exit for[br] end if[br]next[br]if islefted then[br] str2 = str2 & ".."[br]end if[br]cleft = str2[br]end function[br][br][br]function myrandc(n) '生成随机字符,n为字符的个数[br]dim thechr[br]thechr = ""[br]for i=1 to n[br] dim znum,znum2[br] randomize[br] znum = cint(25*rnd)[br] znum2 = cint(10*rnd)[br] if znum2 mod 2 = 0 then[br] znum = znum + 97[br] else[br] znum = znum + 65[br] end if[br] thechr = thechr & chr(znum)[br]next[br]myrandc = thechr[br]end function[br][br][br]function myrandn(n) '生成随机数字,n为数字的个数[br]dim thechr[br]thechr = ""[br]for i=1 to n[br] dim znum,znum2[br] randomize[br] znum = cint(9*rnd)[br] znum = znum + 48[br] thechr = thechr & chr(znum)[br]next[br]myrandn = thechr[br]end function[br][br]function formatquerystr(str) '格式化sql中的like字符串[br]dim nstr[br]nstr = str[br]nstr = replace(nstr,chr(0),"")[br]nstr = replace(nstr,"'","''")[br]nstr = replace(nstr,"[","[[]")[br]nstr = replace(nstr,"%","[%]")[br]formatquerystr = nstr[br]end function[br][br]function getrnd(min,max)[br]randomize[br]getrnd = int((max - min + 1) * rnd + min)[br]end function[br][br]function getrepeattimes(thechar,thestring)[br]getrepeattimes = (len(thestring)-len(replace(thestring,thechar,"")))/len(thechar)[br]end function[br][br]function regreplace(str,patternstr,repstr)[br]dim newstr,regex[br]newstr = str[br]if isnull(newstr) then[br] regreplace = ""[br] exit function[br]end if[br]set regex = new regexp[br]regex.ignorecase = true[br]regex.global = true[br]regex.pattern=patternstr[br]newstr = regex.replace(newstr,repstr)[br]regreplace = newstr[br]end function [br][/p]
该文章在 2010/7/23 0:15:32 编辑过 |
关键字查询
相关文章
正在查询... |