Several ASP useful custom functions

Source: Internet
Author: User
Function

cleft (string,length) returns the specified number of characters from the left side of the string, distinguishing between single and double bytes.

Such as:
Dimmystring,leftstring
mystring= "text Test VBScript"
Leftstring=cleft (mystring,10)
Return to "Text Test vb".


MYRANDC (n) generates random characters, n is the number of characters

Such as:
RESPONSE.WRITEMYRANDN (10)
Output 10 random characters


myrandn (n) generates random numbers, n is the number of numbers

Such as:
RESPONSE.WRITEMYRANDN (10)
Output 10 random digits


formatquerystr (str) formats the like string in SQL.
Such as:
Q=request ("q")
Q=FORMATQUERYSTR (q)
sql= "Select*from[table]whereaalike '%" &q& "%"

getrnd (Min,max) returns a random number between Min-max

Such as:
Response.writegetrnd (100,200)
Output is a random number greater than 100 to 200

Function code:

Functioncleft (str,n)
 dimstr1,str2,alln,islefted
 str2= "
 alln=0
 str1=str
 islefted=false
 ifisnull (str) then
  cleft= "
  exitfunction
 endif
 fori=1tolen (str1)
  nowstr=mid (str1,i,1)
  ifasc (NOWSTR) < 0then
   alln=alln+2
  else
   alln=alln+1
   endif
  if (alln<=n) then
   str2=str2&nowstr
  else
   islefted=true
   exitfor
  endif
 next
  Ifisleftedthen
  str2=str2& "..."
 endif
 cleft=str2
endfunction


FUNCTIONMYRANDC (n) ' generates random characters, n is the number of characters
Dimthechr
Thechr= ""
Fori=1ton
Dimznum,znum2
Randomize
Znum=cint (25*RND)
Znum2=cint (10*RND)
Ifznum2mod2=0then
znum=znum+97
Else
Znum=znum+65
endif
THECHR=THECHR&CHR (Znum)
Next
Myrandc=thechr
Endfunction


FUNCTIONMYRANDN (n) ' generates a random number, n is the number of numbers
Dimthechr
Thechr= ""
Fori=1ton
Dimznum,znum2
Randomize
Znum=cint (9*RND)
znum=znum+48
THECHR=THECHR&CHR (Znum)
Next
Myrandn=thechr
Endfunction

FUNCTIONFORMATQUERYSTR (str) ' format a like string in SQL
Dimnstr
Nstr=str
Nstr=replace (NSTR,CHR (0), "")
Nstr=replace (Nstr, "'", "" ")
Nstr=replace (Nstr, "[", "[[]]")
Nstr=replace (nstr, "%", "[%]")
Formatquerystr=nstr
Endfunction

Functiongetrnd (Min,max)
Randomize
Getrnd=int ((max-min+1) *rnd+min)
Endfunction



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.