📄 stringoperations.asp
字号:
<%
'**************************************************************************************************************
'* License refer to license.txt
'**************************************************************************************************************
'**************************************************************************************************************
'' @CLASSTITLE: StringOperations
'' @CREATOR: Michal Gabrukiewicz - gabru @ grafix.at, Michael Rebec
'' @CREATEDON: 11.12.2003
'' @STATICNAME: str
'' @CDESCRIPTION: Collection of various useful string operations. An instance of this class
'' called "str" is created when loading the page. Thus all methods can easily be
'' accessed using str.methodName.
'' @VERSION: 1.1
'**************************************************************************************************************
class StringOperations
'******************************************************************************************
'' @SDESCRIPTION: returns a full URL with a given file and given parameters
'' @DESCRIPTION: it encodes automatically the values of the parameters.
'' @PARAM: path [string]: the path to the file. e.g. /file.asp, f.asp, http://domain.com/f.asp
'' @PARAM: params [string], [array]: the parameters for the url (querystring). if its an array
'' then every even field is the name and every odd field is the value. if its a string
'' then its treated as jusst one parameter value for the URL. e.g. file.asp?oneValue
'' @PARAM: anker [string]: a jump label. will be appended with # to the end of the URL. empty if no given
'' @RETURN: [string] an URL build with the parameters and fully URL-encoded. example: /file.asp?x=10
'******************************************************************************************
public function URL(path, params, anchor)
URL = path
if isArray(params) then
for i = 0 to uBound(params) step 2
URL = URL & lib.iif(i = 0, "?", "&")
URL = URL & server.URLEncode(params(i)) & "="
if (i + 1) <= uBound(params) then URL = URL & server.URLEncode(params(i + 1))
next
else
URL = URL & "?" & server.URLEncode(params)
end if
if not isEmpty(anchor) then URL = URL & "#" & server.URLEncode(anchor)
end function
'******************************************************************************************
'' @SDESCRIPTION: an extension of the common replace() function. basically the same
'' but you can replace more parts in one go.
'' @DESCRIPTION: find (array) -> replaceWith (string): every match of 'find' will be replaced with the string
'' find (array[n]) -> replaceWith (array[n]): must be same size! every field will be replaced by the field
'' of replaceWith with the same index.
'' find (array[n]) -> replaceWith (array[m]): n > m! for the missing fields the last one will be taken
'' for replacement. e.g. find = array(1, 2), replaceWith = array(3) means that the 1 will be replaced with
'' 3 and the 2 will also be replaced with 3 because there is no equivalent for the 2
'' @PARAM: source [string]: the source string in which we search
'' @PARAM: find [string], [array]: what you are looking for.
'' @PARAM: replaceWith [string], [array]: the value you want to replace the matches with.
'' @RETURN: [string] the replaced source string
'******************************************************************************************
public function change(byVal source, find, replaceWith)
change = source
if isArray(find) then
for i = 0 to uBound(find)
if isArray(replaceWith) then
if uBound(replaceWith) < i then
toReplace = replaceWith(uBound(replaceWith))
else
toReplace = replaceWith(i)
end if
else
toReplace = replaceWith
end if
change = replace(change, find(i), toReplace)
next
elseif not isArray(find) then
change = replace(change, find, replaceWith)
else
lib.error("(str.replace) One of the arguments is of the wrong type.")
end if
end function
'******************************************************************************************
'' @SDESCRIPTION: checks if a given string can be found in a given array
'' @DESCRIPTION: all values in the array are treated as strings when comparing
'' @PARAM: aString [string]: the string which should be checked against
'' @PARAM: anArray [array]: the array with values where the function will walk through
'' @PARAM: caseSensitive [bool]: should the search be case sensitive?
'' @RETURN: [int] index of the first found field within the array. -1 if not found
'******************************************************************************************
public function existsIn(byVal aString, byVal anArray, caseSensitive)
aString = lib.iif(caseSensitive, aString & "", lCase(aString & ""))
for existsIn = 0 to uBound(anArray)
current = lib.iif(caseSensitive, anArray(existsIn) & "", lCase(anArray(existsIn) & ""))
if aString = current then exit function
next
existsIn = -1
end function
'******************************************************************************************************************
'' @SDESCRIPTION: tries to parse a given value into the datatype of the alternative. If it cannot be parsed
'' then the alternative is passed through
'' @DESCRIPTION: it ALWAYS returns the type of the alternative
'' @PARAM: value [string]: value which should be parsed
'' @PARAM: alternative [variant]: alternative value if converting is not possible
'' - if a float value is needed then use a comma. e.g. 0.0
'' @RETURN: [variant] the string parsed into the alternative type or the alternative itself
'******************************************************************************************************************
public function parse(value, alternative)
value = trim(value & "")
parse = alternative
if value = "" then exit function
on error resume next
select case varType(parse)
case 2, 3 'integer, long
parse = cLng(value)
case 4, 5 'single, double
parse = cdbl(value)
case 7 'date
parse = cDate(value)
case 11 'bool
parse = cBool(value)
case else
on error goto 0
lib.throwError("Type not supported for string parsing.")
end select
on error goto 0
end function
'******************************************************************************************************************
'' @SDESCRIPTION: OBSOLETE! use parse() instead
'******************************************************************************************************************
public function toFloat(value, alternative)
toFload = parse(value, alternative)
end function
'******************************************************************************************************************
'' @SDESCRIPTION: OBSOLETE! use parse() instead
'******************************************************************************************************************
public function toInt(value, alternative)
toInt = parse(value, alternative)
end function
'******************************************************************************************************************
'' @SDESCRIPTION: right-aligns a given value by padding left a given character to a totalsize
'' @DESCRIPTION: example: input: 22 -> output: 00022 (padded to total length of 5 with the paddingchar 0)
'' @PARAM: value [string]: the value which should be aligned right
'' @PARAM: totalLength [string]: whats the total Length of the result string
'' @PARAM: paddingChar [string]: the char which is taken for padding
'' @RETURN: [string] right aligned string.
'******************************************************************************************************************
public function padLeft(value, totalLength, paddingChar)
padLeft = right(str.clone(paddingChar, totalLength) & value, totalLength)
end function
'******************************************************************************************************************
'' @SDESCRIPTION: left-aligns a given value by padding right a given character to a totalsize
'' @DESCRIPTION: example: input: 22 -> output: 22000 (padded to total length of 5 with the paddingchar 0)
'' @PARAM: value [string]: the value which should be aligned left
'' @PARAM: totalLength [string]: whats the total Length of the result string
'' @PARAM: paddingChar [string]: the char which is taken for padding
'' @RETURN: [string] left aligned string.
'******************************************************************************************************************
public function padRight(value, totalLength, paddingChar)
padRight = left(value & str.clone(paddingChar, totalLength), totalLength)
end function
'******************************************************************************************************************
'' @SDESCRIPTION: defuses the HTML of given string. so html code wont be recognized as HTML code by browser
'' @PARAM: value [string]: the value which should be defused
'' @RETURN: [string] defused value
'******************************************************************************************************************
public function defuseHTML(value)
'does not work properly ... maybe regex.
defuseHTML = replace(value & "", "<", "<code><</code>")
end function
'******************************************************************************************************************
'' @SDESCRIPTION: makes a given string safe for the use within sql statements
'' @DESCRIPTION: e.g. if its necessary to pass through an user input directly into a sql-query
'' @PARAM: value [string]: the value which should be made "safe"
'' @RETURN: [string] safe value. e.g. ' are escaped with '', etc.
'******************************************************************************************************************
public function SQLSafe(value)
SQLSafe = replace(value & "", "--", "")
SQLSafe = replace(SQLSafe & "", "'", "''")
end function
'******************************************************************************************************************
'' @DESCRIPTION: Makes a string javascript persistent. Changes special characters, etc.
'' @DESCRIPTION: using this function it has to be possible to pass any string which should be
'' executed in a javascript later. example: you want to execute the following
'' onclick="obj.value='" & usrInput & "'. so in this case usrInput needs to be validated that no
'' javascript error happens when he/she enters e.g. a '.
'' @PARAM: val [string]: the value which needs to be encoded
'' @RETURN: [string] encoded string which can be used within javascript strings.
'******************************************************************************************************************
public function JSEncode(byVal val)
val = val & ""
tmp = replace(tmp, chr(92), "\\")
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -