String operation function example in asp vb
Use string functions to intercept, remove, and replace uppercase and lowercase strings. Function syntax
Len (string | varname) returns the number of characters in the string or the number of bytes required to store a variable.
Trim (string) removes spaces before and after the string
Ltrim (string) removes spaces before the string
Rtrim (string) removes spaces after the string
Mid (string, start, length) gets the length string starting from the start character of the string. If the third parameter is omitted, it indicates the string starting from the start character to the end of the string.
Left (string, length) gets the length string from the Left side of the string.
Right (string, length) gets the length string from the Right side of the string.
LCase (string) converts all uppercase letters in the string to lowercase letters.
UCase (string) converts all uppercase letters in the string to uppercase letters.
StrComp (string1, string2 [, compare]) returns the comparison result between the string1 string and the string2 string. If the two strings are the same, 0 is returned. If the two strings are less than 1,-1 is returned, if the value is greater than 1, 1 is returned.
InStr (string1, string2 [, compare]) returns the position where the string1 string first appears in the string2 string.
Split (string1, delimiter [, count [, start]) Splits a string into one-dimensional arrays based on delimiter. delimiter is used to identify the substring boundary. If omitted, use space ("") as the separator.
Count returns the number of substrings.-1 indicates that all substrings are returned.
Start is 1. If it is 0, or the binary comparison is omitted.
Replace (expression, find, replacewith [, compare [, count [, start]) returns a string, where a specified number of substrings (find) replacewith ).
1. Len function example:
The following example uses the Len function to return the number of characters in a string:
Dim MyString
MyString = Len ("VBSCRIPT") 'mystring contains 8.
2. Trim, Ltrim, and Rtrim function examples:
The following example uses the LTrim, RTrim, and Trim functions to remove the leading space, trailing space, starting space, and trailing space of a string:
Dim MyVar
MyVar = LTrim ("vbscript") 'myvar contains "vbscript ".
MyVar = RTrim ("vbscript") 'myvar contains "vbscript ".
MyVar = Trim ("vbscript") 'myvar contains "vbscript ".
3. Mid function example:
The following example uses the Mid function to return the six characters starting from the fourth character in the string:
Dim MyVar
MyVar = 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 letters on the Left of MyString:
Dim MyString, LeftString
MyString = "VBSCript"
LeftString = 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 side of the string:
Dim AnyString, MyStr
AnyString = "Hello World" 'defines a string.
MyStr = Right (AnyString, 1) 'returns "d ".
MyStr = Right (AnyString, 6) 'returns "World ".
MyStr = Right (AnyString, 20) 'returns "Hello World ".
6. LCase function example:
The following example uses the LCase function to convert uppercase letters to lowercase letters:
Dim MyString
Dim LCaseString
MyString = "VBSCript"
LCaseString = LCase (MyString) 'lcasestring contains "vbscript ".
7. UCase function example:
The following example uses the UCase function to return the string in uppercase:
Dim myword
Myword = ucase ("Hello World") 'returns "Hello World ".
8. strcomp function example:
The following example uses the strcomp function to return the result of string comparison. If the third parameter is 1, text comparison is executed. If the third parameter is 0, binary comparison is executed.
Dim mystr1, mystr2, mycomp
Mystr1 = "ABCD": mystr2 = "ABCD" 'defines the variable.
Mycomp = strcomp (mystr1, mystr2, 1) 'returns 0.
Mycomp = strcomp (mystr1, mystr2, 0) 'returns-1.
Mycomp = strcomp (mystr2, mystr1) 'returns 1.
9. instr example:
The following example uses instr to search for strings:
Dim searchstring, searchchar, Mypos
Searchstring = "xxpxxpxxpxxp "???
Searchchar = "P "?
Mypos = instr (searchstring, searchchar )??? 'Return 9.
Note: The returned result is not the first occurrence of a character string in another string, but the byte position.
10. 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") 'to obtain the A12D