String functions are a great way to manipulate and parse the data within your strings, below are some of the most commonly used ones. For our purposes, we will use this variable as an example:
strExample = "These are examples of string functions."
InStr
<% DIM strPosition strPosition = InStr(1, strExample, "examples", 1) Response.Write strPosition %>
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.
Left
<% DIM strLeftText strLeftText = Left(strExample, 18) Response.Write strLeftText %>
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.
Right
<% DIM strRightText strRightText = Right(strExample, 10) Response.Write strRightText %>
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.
Mid
<% DIM strMidText strMidText = Mid(strExample, 11, 8) Response.Write strMidText %>
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.
Trim, LTrim, and RTrim
<% DIM strTrimText, strLTrimText, strRTrimText strTrimText = Trim(strExample) strLTrimText = LTrim(strExample) strRTrimText = RTrim(strExample) Response.Write strTrimText Response.Write strLTrimText Response.Write strRTrimText %>
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.
LCase and UCase
<% DIM strLCaseText, strUCaseText strLCaseText = LCase(strExample) strUCaseText = UCase(strExample) Response.Write strLTrimText Response.Write strRTrimText %>
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.
Len
<% DIM intLenText intLenText = Len(strExample) Response.Write intLenText %>
In this example, intLenExample returns the value: " 39 " . The Len function returns the number of characters in a given string.
String
<% DIM strMyExample strMyExample = String(1, 97) Response.Write strMyExample %>
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.
http://www.aspwebpro.com/aspscripts/functions/string.asp
No comments:
Post a Comment