ASP中一个字符串处理类
 代码如下:

<%
class stringoperations
''****************************************************************************
'''' @功能说明: 把字符串换为char型数组
'''' @参数说明:  - str [string]: 需要转换的字符串
'''' @返回值:   - [array] char型数组
''****************************************************************************
public function tochararray(byval str)
 redim chararray(len(str))
 for i = 1 to len(str)
  chararray(i-1) = mid(str,i,1)
 next
 tochararray = chararray
end function
''****************************************************************************
'''' @功能说明: 把一个数组转换成一个字符串
'''' @参数说明:  - arr [array]: 需要转换的数据
'''' @返回值:   - [string] 字符串
''****************************************************************************
public function arraytostring(byval arr)
 for i = 0 to ubound(arr)
  strobj = strobj & arr(i)
 next
 arraytostring = strobj
end function
''****************************************************************************
'''' @功能说明: 检查源字符串str是否以chars开头
'''' @参数说明:  - str [string]: 源字符串
'''' @参数说明:  - chars [string]: 比较的字符/字符串
'''' @返回值:   - [bool]
''****************************************************************************
public function startswith(byval str, chars)
 if left(str,len(chars)) = chars then
  startswith = true
 else
  startswith = false
 end if
end function
''****************************************************************************
'''' @功能说明: 检查源字符串str是否以chars结尾
'''' @参数说明:  - str [string]: 源字符串
'''' @参数说明:  - chars [string]: 比较的字符/字符串
'''' @返回值:   - [bool]
''****************************************************************************
public function endswith(byval str, chars)
 if right(str,len(chars)) = chars then
  endswith = true
 else
  endswith = false
 end if
end function
''****************************************************************************
'''' @功能说明: 复制n个字符串str
'''' @参数说明:  - str [string]: 源字符串
'''' @参数说明:  - n [int]: 复制次数
'''' @返回值:   - [string] 复制后的字符串
''****************************************************************************
public function clone(byval str, n)
 for i = 1 to n
  value = value & str
 next
 clone = value
end function
''****************************************************************************
'''' @功能说明: 删除源字符串str的前n个字符
'''' @参数说明:  - str [string]: 源字符串
'''' @参数说明:  - n [int]: 删除的字符个数
'''' @返回值:   - [string] 删除后的字符串
''****************************************************************************
public function trimstart(byval str, n)
 value = mid(str, n+1)
 trimstart = value
end function
''****************************************************************************
'''' @功能说明: 删除源字符串str的最后n个字符串
'''' @参数说明:  - str [string]: 源字符串
'''' @参数说明:  - n [int]: 删除的字符个数
'''' @返回值:   - [string] 删除后的字符串
''****************************************************************************
public function trimend(byval str, n)
 value = left(str, len(str)-n)
 trimend = value
end function
''****************************************************************************
'''' @功能说明: 检查字符character是否是英文字符 a-z or a-z
'''' @参数说明:  - character [char]: 检查的字符
'''' @返回值:   - [bool] 如果是英文字符,返回true,反之为false
''****************************************************************************
public function isalphabetic(byval character)
 asciivalue = cint(asc(character))
 if (65 <= asciivalue and asciivalue <= 90) or (97 <= asciivalue and asciivalue <= 122) then
  isalphabetic = true
 else
  isalphabetic = false
 end if
end function
''****************************************************************************
'''' @功能说明: 对str字符串进行大小写转换
'''' @参数说明:  - str [string]: 源字符串
'''' @返回值:   - [string] 转换后的字符串
''****************************************************************************
public function swapcase(str)
 for i = 1 to len(str)
  current = mid(str, i, 1)
  if isalphabetic(current) then
   high = asc(ucase(current))
   low = asc(lcase(current))
   sum = high + low
   return = return & chr(sum-asc(current))
  else
   return = return & current
  end if
 next
 swapcase = return
end function
''****************************************************************************
'''' @功能说明: 将源字符串str中每个单词的第一个字母转换成大写
'''' @参数说明:  - str [string]: 源字符串
'''' @返回值:   - [string] 转换后的字符串
''****************************************************************************
public function capitalize(str)
 words = split(str," ")
 for i = 0 to ubound(words)
  if not i = 0 then
   tmp = " "
  end if
  tmp = tmp & ucase(left(words(i), 1)) & right(words(i), len(words(i))-1)
  words(i) = tmp
 next
 capitalize = arraytostring(words)
end function
''****************************************************************************
'''' @功能说明: 将源字符str后中的''过滤为''''
'''' @参数说明:  - str [string]: 源字符串
'''' @返回值:   - [string] 转换后的字符串
''****************************************************************************
public function checkstr(str)
 if trim(str)="" or isnull(str) then
  checkstr=""
 else
  checkstr=replace(trim(str),"''","''''")
 end if
end function
''****************************************************************************
'''' @功能说明: 将字符串中的str中的html代码进行过滤
'''' @参数说明:  - str [string]: 源字符串
'''' @返回值:   - [string] 转换后的字符串
''****************************************************************************
public function htmlencode(str)
 if trim(str)="" or isnull(str) then
  htmlencode=""
 else
  str=replace(str,">",">")
  str=replace(str,"<","<")
  str=replace(str,chr(32)," ")
  str=replace(str,chr(9)," ")
  str=replace(str,chr(34),""")
  str=replace(str,chr(39),"'")
  str=replace(str,chr(13),"")
  str=replace(str,chr(10) & chr(10), "

")
  str=replace(str,chr(10),"
 ")
  htmlencode=str
 end if
end function
''****************************************************************************
'''' @功能说明: 计算源字符串str的长度(一个中文字符为2个字节长)
'''' @参数说明:  - str [string]: 源字符串
'''' @返回值:   - [int] 源字符串的长度
''****************************************************************************
public function strlen(str)
 if trim(str)="" or isnull(str) then
  strlen=0
 else
  dim p_len,x
  p_len=0
  strlen=0
  p_len=len(trim(str))
  for x=1 to p_len
   if asc(mid(str,x,1))<0 then
    strlen=int(strlen) + 2
   else
    strlen=int(strlen) + 1
   end if
  next
 end if
end function
''****************************************************************************
'''' @功能说明: 截取源字符串str的前lennum个字符(一个中文字符为2个字节长)
'''' @参数说明:  - str [string]: 源字符串
'''' @参数说明:  - lennum [int]: 截取的长度
'''' @返回值:   - [string]: 转换后的字符串
''****************************************************************************
public function cutstr(str,lennum)
 dim p_num
 dim i,x
 if strlen(str)<=lennum then
  cutstr=str
 else
  p_num=0
  x=0
  do while not p_num > lennum-2
   x=x+1
   if asc(mid(str,x,1))<0 then
    p_num=int(p_num) + 2
   else
    p_num=int(p_num) + 1
   end if
   cutstr=left(trim(str),x)&"..."
  loop
 end if
end function
end class
%>

相关文章