Example of an ASP string function
Use String functions to truncate the string to the end, case substitution, and so on.
Function |
Grammar |
Function |
Len |
Len (String|varname) |
Returns the number of characters within a string, or the number of bytes required to store a variable. |
Trim |
Trim (String) |
Remove the space before and after the string |
Ltrim |
Ltrim (String) |
Remove the space before the string |
Rtrim |
Rtrim (String) |
Remove the space after the string |
Mid |
Mid (String,start,length) |
Gets a length string from the start character of the string string, if the third argument is omitted to represent a string starting from the start character to the end of the string |
Left |
Left (String,length) |
To get a length string from the left side of a string string |
Right |
Right (String,length) |
To get a length string from the right of a string string |
LCase |
LCase (String) |
Converts all uppercase letters in string strings to lowercase letters |
UCase |
UCase (String) |
Converts all uppercase letters in a string string to uppercase letters |
StrComp |
StrComp (String1,string2[,compare]) |
Returns a comparison between the string1 string and the string2 string, if two strings are the same, returns 0 if less than 1, or 1 if greater than |
InStr |
INSTR (String1,string2 [, compare] ) |
Returns the position of the first occurrence of the string1 string in the string2 string |
Split |
Split (String1,delimiter [, count[, start] ]) |
Splits a string into a one-dimensional array based on delimiter, where delimiter is used to identify the substring bounds. If omitted, use a space ("") as the separator. The number of substrings returned by count,-1 indicates that all substrings are returned. Start is 1 to perform a text comparison, if 0 or omit to perform a binary comparison. |
Replace |
Replace(expression, find, replacewith[, compare[, count[, start]]]) |
Returns a string in which a specified number of substrings (find) is replaced with another substring (replacewith). |
|
|
|
|
|
|
1. Len Function Example:
The following example uses the Len function to return the number of characters in a string:
Len("VBSCRIPT") 'MyString
Contains 8
. 2, Trim, Ltrim, RTrim function Example:
The following example uses the LTrim, RTrim, and Trim functions to remove spaces, trailing spaces, start, and trailing spaces at the beginning of a string, respectively:
LTrim(" vbscript ") 'MyVar
Contains "vbscript "
. RTrim(" vbscript ") 'MyVar
contains " vbscript"
. Trim(" vbscript ") 'MyVar
contains "vbscript"
.
3, Mid Function Example:
The following example uses the Mid function to return six characters starting from the fourth character in a string:
Mid("VB
Script is fun!", 4, 6) 'MyVar
contains "Script"
.
4, Left Function example:
The following example uses the left function to return the three-letter mystring:
Left(MyString, 3) 'LeftString
Contains "VBS
5, Right Function example:
The following example uses the right function to return a specified number of characters from the right-hand side of a string:
Dim AnyString, MyStrAnyString = "Hello World" '
Defines a string. Right(AnyString, 1) '
return "d"
. Right(AnyString, 6) '
return " World"
. Right(AnyString, 20) '
return "Hello World"
. 6, LCase Function Example:
The following example uses the LCase function to convert the uppercase letter to lowercase letters:
LCase(MyString) ' LCaseString
Contains "vbscript"
. 7, UCase Function Example:
The following example uses the UCase function to return the uppercase form of a string:
UCase("Hello World") '
Return "HELLO WORLD"
.
8, StrComp Function Example:
The following example uses the StrComp function to return the result of a string comparison. If the third argument is 1 to perform a text comparison, if the third argument is 0 or omit to perform a binary comparison.
Dim MyStr1, MyStr2, MyCompMyStr1 = "ABCD": MyStr2 = "abcd" '
Define variables. StrComp(MyStr1, MyStr2, 1) '
return 0
. StrComp(MyStr1, MyStr2, 0) '
return -1
. StrComp(MyStr2, MyStr1) '
return 1
.
9, instr Example:
The following example uses InStr to search for strings:
Dim SearchString, SearchChar, MyPosSearchString ="XXpXXpXXPXXP"???SearchChar = "P"??
Instr(SearchString, SearchChar)
???'返回 9.
Note: Instead of returning the character position where the string first appears in another string, the byte position is not returned.
10, the Split function example:
Dim MyString, MyArray, MSG
MyString = "vbscriptxisxfun!"
MyArray = Split (MyString, "X", -1,1)
' MyArray (0) contains "VBScript".
' MyArray (1) contains ' is '.
' MyArray (2) contains "fun!".
Response.Write (myarray (0))
11, replace function Example:
Replace ("ABCD", "BC", "12") ' Get a12d