Example of an ASP string function

Source: Internet
Author: User
Tags comparison count functions lowercase rtrim split trim uppercase letter
Functions | example | String ASP's String function example
Use String functions to truncate the string to the end, case substitution, and so on. function Syntax 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) to remove spaces before and after a string
Ltrim Ltrim (String) removes the space before the string
Rtrim Rtrim (String) to 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 string,length the length of a string from the left-hand side of a string string
Right-hand right (string,length) Gets the length of a string from the side of the string string
LCase LCase (String) converts all uppercase letters in string strings to lowercase letters
UCase UCase (String) converts all uppercase letters in string strings to uppercase letters
StrComp StrComp (String1,string2[,compare]) returns a comparison of string1 string and 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 first occurrence of the string1 string in the string2 string
Split Split (string1,delimiter[, count[, start]) splits the 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:

Dim MyString
MyString = 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:

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 six characters starting from the fourth character in a 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-letter 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-hand side of a string:

Dim AnyString, MyStr
anystring = "Hello World" defines a string.
MyStr = Right (anystring, 1) ' returns to ' d '.
MyStr = Right (anystring, 6) ' returns to World '.
MyStr = Right (anystring, 20) ' returns to ' Hello world '.


6, LCase Function Example:

The following example uses the LCase function to convert the uppercase letter 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 uppercase form of a string:

Dim MyWord
MyWord = UCase ("Hello World") ' returns to ' 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, MyComp
MYSTR1 = "ABCD": MyStr2 = "ABCD" defines variables.
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)??? ' Returns 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




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.