Classic ASP String Functions

String functions are a great way to manipulate and parse the data within your strings, below are some of the most commonly used ones.

InStr:
In this example, our script returns the value: 11 . The InStr function is a powerful tool that searches text strings and returns the position of a given text substring. In this case, we search the strPosition string for the "example" and our script returns "11", which is the starting position of the word within the string.

<%
Dim strPosition
Dim strExample

strExample	= "These are examples of string functions."
strPosition	= InStr(1, strExample, "examples", 1)

Response.Write( strPosition )
%>

Left:
In this example, our script returns the value: " These are examples ". The Left function takes a string and returns a specified number of characters starting from the left side.

<%
Dim strLeftText
Dim strExample

strExample	= "These are examples of string functions."
strLeftText	= Left(strExample, 18)

Response.Write( strLeftText )
%>

Right:
In this example, our script returns the value: " functions. ". The Right function takes a string and returns a specified number of characters starting from the end of the string.

<%
Dim strRightText
Dim strExample

strExample	= "These are examples of string functions."
strRightText	= Right(strExample, 10)

Response.Write( strRightText )
%>

Mid:
In this example, our script returns the value: " examples ". The Right function takes a string and returns a specified number of characters starting from the end of the string.

<%
Dim strMidText
Dim strExample

strExample	= "These are examples of string functions."
strMidText	= Mid(strExample, 11, 8)

Response.Write( strMidText )
%>

Trim, LTrim, and RTrim:
In this example, all three atrTrimText, strLTrimText, and strRTrimText return our original example: "These are examples of string functions." LTrim is used to remove blank spaces at the beginning of a string. RTrim is used to remove blank spaces at the end of a string. Trim is used to remove spaces from both the beginning and the end of a string.

<%
Dim strTrimText
Dim strLTrimText
Dim strRTrimText
Dim strExample

strExample	= "These are examples of string functions."
strTrimText	= Trim(strExample)
strLTrimText	= LTrim(strExample)
strRTrimText	= RTrim(strExample)

Response.Write( strTrimText )
Response.Write( strLTrimText )
Response.Write( strRTrimText )
%>

LCase and UCase:
In this example, strLCaseText returns the value: " these are examples of string functions. " and strUCaseText returns the value: " THESE ARE EXAMPLES OF STRING FUNCTIONS. ". As you can tell, the LCase function returns all lower case letters and the UCase function returns all upper case letters. These functions are especially useful for inserting and updating records in your database because they help keep your data consistent.

<%
Dim strLCaseText
Dim strUCaseText
Dim strExample

strExample	= "These are examples of string functions."
strLCaseText	= LCase(strExample)
strUCaseText	= UCase(strExample)

Response.Write( strLTrimText )
Response.Write( strRTrimText )
%>

Len:
In this example, intLenExample returns the value: " 39 " . The Len function returns the number of characters in a given string.

<%
Dim intLenText
Dim strExample

strExample	= "These are examples of string functions."
intLenText	= Len(strExample)

Response.Write( intLenText )
%>

String:
In this example, strMyExample returns the value: " a " . The String function takes a character code (97) and returns its character (a). The (1) in this case controls how many time the "a" will be displayed.

<%
Dim strMyExample

strMyExample	= String(1, 97)

Response.Write( strMyExample )
%>

Bookmark

Advertisement