ASP中一個字串處理類(VBScript) 原版

來源:互聯網
上載者:User

這個類是用於處理字串的,是老外寫的,我把裡面的功能和參數加了說明

使用方法:

=============== test.asp================

<!--#include file="StringOperations.asp"-->

<%
dim str
set str = New StringOperations
 test = str.toCharArray("check this out")
 response.write "<strong>str.toCharArray</strong>: "
 for i = 0 to ubound(test)
  response.write test(i) & " "
 next
 
 response.write "<BR><BR>"
 test1 = str.arrayToString(test)
 response.write "<strong>str.arrayToString</strong>: " & test1
 
 response.write "<BR><BR>"
 response.write "<strong>str.startsWith</strong>: " & str.startsWith(test1, "ch")
 
 response.write "<BR><BR>"
 response.write "<strong>str.endWith</strong>: " & str.endsWith(test1, "out")
 
 response.write "<BR><BR>"
 response.write "<strong>str.clone</strong>: " & str.clone("abc", 10)
 
 response.write "<BR><BR>"
 response.write "<strong>str.trimStart</strong>: " & str.trimStart(test1, 3)
 
 response.write "<BR><BR>"
 response.write "<strong>str.trimEnd</strong>: " & str.trimEnd(test1, 2)
 
 response.write "<BR><BR>"
 response.write "<strong>str.swapCase</strong>: " & str.swapCase("HiHiHi")
 
 response.write "<BR><BR>"
 response.write "<strong>str.isAlphabetic</strong>: " & str.isAlphabetic("!")
 
 response.write "<BR><BR>"
 response.write "<strong>str.capitalize</strong>: " & str.capitalize("clara fehler")
Set str = Nothing
%>

=============== StringOperations.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

end class
%>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.